之前在项目中遇到了需要实现文件下载的方法,在网络上查找资料后,发现了下面这种通过Blob和XMLHttpRequest实现文件下载的方法。
非跨域文件的下载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| function getBlob(url) { return new Promise(resolve => { const xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'blob'; xhr.onload = () => { if (xhr.status === 200) { resolve(xhr.response); } }; xhr.send(); }); }
function saveAs(blob, filename) { if (window.navigator.msSaveOrOpenBlob) { navigator.msSaveBlob(blob, filename); } else { const link = document.createElement('a'); link.href = window.URL.createObjectURL(blob); link.download = filename; link.click(); window.URL.revokeObjectURL(link.href); } }
function download(url, filename) { getBlob(url).then(blob => { saveAs(blob, filename); }); }
download('https://github.com/vuejs/vue-router', 'vue-router.html');
|
上边这种方式可以解决非跨域文件的下载问题,但是遇到跨域文件时就无能为力了。
跨域文件的下载
后来,我又继续查了下资料,找到了下面这种方式,可以解决跨域文件的下载问题。
这里直接上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
function saveAs(data, name) { const urlObject = window.URL || window.webkitURL || window; const export_blob = new Blob([data]); const save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a'); save_link.href = urlObject.createObjectURL(export_blob); save_link.download = name; save_link.click(); }
function getFileName(url) { const num = url.lastIndexOf('/') + 1; let fileName = url.substring(num); fileName = decodeURI(fileName.split('?')[0]); return fileName; }
function downloadUrlFile(url) { const newUrl = url.replace(/\\/g, '/'); const xhr = new XMLHttpRequest(); xhr.open('GET', newUrl, true); xhr.responseType = 'blob'; xhr.onload = () => { if (xhr.status === 200) { const fileName = getFileName(newUrl); saveAs(xhr.response, fileName); } };
xhr.send(); }
export default downloadUrlFile;
|