下载文件到本地(支持图片,视频,语音,压缩包,安装包,文档等等)
// row.fullPath文件地址 http://192.168.x.xx:8080/20230529iPhone127E0C8CB522C9.MP4
fetch(row.fullPath)
.then(res => res.blob())
.then(blob => {
const a = document.createElement('a')
document.body.appendChild(a)
a.style.display = 'none'
const url = window.URL.createObjectURL(blob)
a.href = url
a.download = row.fileName //文件名称
a.click()
document.body.removeChild(a)
window.URL.revokeObjectURL(url)
})
1.发起HTTP请求
2.将响应转换为BLob
3.响应对象blob方法将响应的数据转换为一个Blob对象,Blob对象主要用于表示不可变的、原始数据的类文件对象
4.创建隐藏的a标签
5.创建URL对象const url = window.URL.createObjectURL(blob) 通过其创建一个表示指向Blob对象的url
6.a设置href和download以便下载使
7.模拟a的点击事件,移除a标签,并通过revokeObjectURL释放创建的URL,以免内存泄露哦