|
@@ -11,12 +11,29 @@ export default {
|
|
|
* @param {*} fileName
|
|
* @param {*} fileName
|
|
|
*/
|
|
*/
|
|
|
downloadFile: function (url, fileName) {
|
|
downloadFile: function (url, fileName) {
|
|
|
- var a = document.createElement('a');
|
|
|
|
|
- a.download = fileName;
|
|
|
|
|
- a.href = url;
|
|
|
|
|
- $('body').append(a); // 修复firefox中无法触发click
|
|
|
|
|
- a.click();
|
|
|
|
|
- $(a).remove();
|
|
|
|
|
|
|
+ var xhr = new XMLHttpRequest();
|
|
|
|
|
+ xhr.open('get', url, true);
|
|
|
|
|
+ const token = localStorage.getItem('#token');
|
|
|
|
|
+ xhr.setRequestHeader('token', token);
|
|
|
|
|
+ xhr.setRequestHeader('Content-type', 'application/json');
|
|
|
|
|
+ xhr.responseType = 'blob';
|
|
|
|
|
+ xhr.onreadystatechange = function(){
|
|
|
|
|
+ if(xhr.readyState === 4 && xhr.status === 200){
|
|
|
|
|
+ let res = xhr.response;
|
|
|
|
|
+ let type = xhr.getResponseHeader('Content-Type');
|
|
|
|
|
+ let blob = new Blob([res], { type: type });
|
|
|
|
|
+ const blobUrl = URL.createObjectURL(blob);
|
|
|
|
|
+ const link = document.createElement('a');
|
|
|
|
|
+ link.download = fileName;
|
|
|
|
|
+ link.style.display = 'none';
|
|
|
|
|
+ link.href = blobUrl;
|
|
|
|
|
+ document.body.appendChild(link);
|
|
|
|
|
+ link.click();
|
|
|
|
|
+ URL.revokeObjectURL(blobUrl);
|
|
|
|
|
+ document.body.removeChild(link);
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ xhr.send();
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
|
|
|