接口返回结果如此
代码
exportByBase64(response) {
// const bloburl = dataURIToBlob(response.file)
var downloadElement = document.createElement("a");
// downloadElement.href = URL.createObjectURL(bloburl);
downloadElement.href = response.file;
downloadElement.download = response.filename; //下载后文件名
document.body.appendChild(downloadElement);
downloadElement.click(); //点击下载
document.body.removeChild(downloadElement); //下载完成移除元素
window.URL.revokeObjectURL(downloadElement.href); //释放掉blob对象
},
问题
当文件太大时,会因为a标签的href属性长度上限导致最多只能下载近4m左右的文件,当文件过大时, 要将base64转化为blob格式,再URL.createObjectURL转为url,用a标签打开
function dataURIToBlob(dataURI) {
var binStr = atob(dataURI.split(',')[1]),
len = binStr.length,
arr = new Uint8Array(len);
for (var i = 0; i < len; i++) {
arr[i] = binStr.charCodeAt(i);
}
return new Blob([arr])
}
exportByBase64(response) {
const bloburl = dataURIToBlob(response.file)
var downloadElement = document.createElement("a");
downloadElement.href = URL.createObjectURL(bloburl);
downloadElement.download = response.filename; //下载后文件名
document.body.appendChild(downloadElement);
downloadElement.click(); //点击下载
document.body.removeChild(downloadElement); //下载完成移除元素
window.URL.revokeObjectURL(downloadElement.href); //释放掉blob对象
},