methods:{ //下載文件 filerightDown(index,fileName) {//index 接口參數 fileName文件名字 var _this = this; var fileName = fileName; this.$http({ method: "post", url: this.HOST + api.download, params: { fileId: index }, responseType:'arraybuffer' }) .then(res => { this.download(res.data,fileName); }) .catch(req => { console.log("下載失敗", req); }); }, // 下載文件 download (data,fileName) { if (!data) { return } let url = window.URL.createObjectURL(new Blob([data])); let link = document.createElement('a'); link.style.display = 'none'; link.href = url; link.setAttribute('download', fileName); document.body.appendChild(link); link.click(); }, }
responseType:'arraybuffer'
XMLHttpRequest.responseType 屬性是一個枚舉類型的屬性,返回響應數據的類型。它允許我們手動的設置返回數據的類型。如果我們將它設置為一個空字符串,它將使用默認的"text"類型。
簡而言之, responseType 的作用就是設置ajax 數據響應的類型, 你告訴服務器,讓服務器返回什么樣的數據類型給你;
new BlobBlob
對象表示一個不可變、原始數據的類文件對象。Blob 表示的不一定是JavaScript原生格式的數據。File
接口基於Blob
,繼承了 blob 的功能並將其擴展使其支持用戶系統上的文件。