We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
<a>
href
download
click
fetch('http://somehost/somefile.zip').then(res => res.blob().then(blob => { var a = document.createElement('a'); var url = window.URL.createObjectURL(blob); var filename = 'myfile.zip'; a.href = url; a.download = filename; // 此处firefox需特殊处理 或使用dispatchEvent // document.body.appendChild(a); // a.click(); // document.body.removeChild(a); //https://blog.csdn.net/qq_41960675/article/details/83782347 a.click(); window.URL.revokeObjectURL(url); }));
createObjectURL
blob
object URL
src
revokeObjectURL
ps.打开新页面下载的话window.open(url)
window.open(url)
由于某些文件下载需要 通过请求头加token的方式 来鉴权, 即无法常规通过a标签下载 (get 请求 服务端直接返回二进制文件)
所以通过ajax方式下载, 并注意设置返回类型为 blob
// 例如 axios 中 request({ url: '/some/download', method: 'get', responseType: "blob" })
// 返回的blob 直接通过 URL.createObjectURL(blob) 设置链接 // 或通过以下方法 传入 res export function resolveBlob(res, mimeType, fileName) { const aLink = document.createElement('a') var blob = new Blob([res], { type: mimeType }) fileName = fileName.replace(/\"/g, '') const url = URL.createObjectURL(blob) aLink.href = url aLink.setAttribute('download', fileName) // 设置下载文件名称 document.body.appendChild(aLink) aLink.click() document.body.removeChild(aLink) URL.revokeObjectURL(url) }
下载的是图片的话给a标签添加download属性需要该图片为同源,非同源可先请求下载再转换
a标签
getBase64Img(url){ return new Promise((resolve, reject) => { let xhr = new XMLHttpRequest() xhr.open('get', url, true) xhr.responseType = 'blob' xhr.onload = function() { if (this.status === 200) { let blob = this.response let fileReader = new FileReader() fileReader.onloadend = function(e) { let result = e.target.result resolve(result) } fileReader.readAsDataURL(blob) } } xhr.onerror = function() { reject() } xhr.send() }) }
参考
The text was updated successfully, but these errors were encountered:
下载文件pdf 浏览器直接预览却并非下载
先尝试给a标签设置download属性
或尝试设置文件 响应头部 Content-Disposition: attachment
Content-Disposition: attachment
Sorry, something went wrong.
No branches or pull requests
js下载文件
思路
<a>
标签href
属性download
属性click
事件createObjectURL
用blob
对象来创建一个object URL
(它是一个 DOMString),我们可以用这个object URL
来表示某个blob
对象,这个object URL
可以用在href
和src
之类的属性上。revokeObjectURL
释放由createObjectURL
创建的object URL
,当该object URL
不需要的时候,我们要主动调用这个方法来获取最佳性能和内存使用。由于某些文件下载需要 通过请求头加token的方式 来鉴权, 即无法常规通过a标签下载 (get 请求 服务端直接返回二进制文件)
所以通过ajax方式下载, 并注意设置返回类型为 blob
下载的是图片的话给
a标签
添加download
属性需要该图片为同源,非同源可先请求下载再转换参考
The text was updated successfully, but these errors were encountered: