實現一個post方式的檔案下載
function export() {
let url = 'url';
this.$ajax.post(url, {id: 1234}, {responseType: 'arraybuffer'}).then(res => {
let bytes = new Uint16Array(res);
let len = bytes.byteLength;
if (len <= 82) {
let result = arrayBufferToString(res);
let strResult = JSON.parse(result);
console.log(strResult);
} else {
let blob = new Blob([res], {type: "application/vnd.ms-excel"});
let objectUrl = URL.createObjectURL(blob);
let a = document.createElement('a');
document.body.appendChild(a);
a.setAttribute('style', 'display:none');
a.setAttribute('href', objectUrl);
a.setAttribute('download', '事件列表.xlsx');
a.click();
URL.revokeObjectURL(objectUrl);
}
})
}
function arrayBufferToString(buffer) {
let str = String.fromCharCode.apply(null, new Uint8Array(buffer));
return decodeURIComponent(escape(str));
}
複製程式碼