fileLinkToStreamDownload(url, ‘文件名’, ‘pdf’)
function fileLinkToStreamDownload (url, fileName, type) {
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.setRequestHeader('Content-Type', `application/` + type);
xhr.responseType = "blob";
xhr.onload = function () {
if (this.status == 200) {
var blob = this.response;
downloadExportFile(blob, fileName, type)
}
}
xhr.send();
}
function downloadExportFile (blob, tagFileName, fileType) {
var downloadElement = document.createElement('a');
var href = blob;
if (typeof blob == 'string') {
downloadElement.target = '_blank';
} else {
href = window.URL.createObjectURL(blob); //创建下载的链接
}
downloadElement.href = href;
downloadElement.download = tagFileName + '.' + fileType; //下载后文件名
document.body.appendChild(downloadElement);
downloadElement.click(); //点击下载
document.body.removeChild(downloadElement); //下载完成移除元素
if (typeof blob != 'string') {
window.URL.revokeObjectURL(href); //释放掉blob对象
}
}