网上无数下载的方法,先挨个试试看好不好用,不好用再用此方法
ajax(url, callback, options) {
window.URL = window.URL || window.webkitURL;
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
if (options.responseType) {
xhr.responseType = options.responseType;
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
callback(xhr);
}
};
xhr.send();
},
downloadAjax() {
let innerurl = this.url;// 文件地址
var name = this.url.replace(/(.*\/)*([^.]+).*/ig, '$2');
this.ajax(innerurl, function (xhr) {
let filename = name + '.' + innerurl.replace(/(.*\.)/, '');
let content = xhr.response;
let a = document.createElement('a');
let blob = new Blob([xhr.response]);
let url = window.URL.createObjectURL(blob);
a.href = url;
a.download = filename;
a.click();
window.URL.revokeObjectURL(url);
}, {
responseType: 'blob'
});
},
download() {
if (//是url) {
this.downloadAjax();
}
if (//是下载接口) {
// 调用接口
// 携带下载请求参数
config: {
download: true
}
}