需求:上傳excel表格到服務器,然後接收返回的結果(二進制)並轉成excel表格下載
實施:採用 Element-UI的uploade 組件,功能較全且有完整的生命週期鉤子可設置。
<el-upload
class="upload-demo"
ref="upload"
action=""
:on-remove="handleRemove"
:on-error="handleError"
:file-list="fileList"
:auto-upload="false"
:limit=1
>
<el-button
slot="trigger"
size="small"
type="primary"
>選取文件</el-button>
</el-upload>
問題1:post請求,不能用window.open(_url)的方式觸發瀏覽器下載。
解決方法:blob對象,將二進制轉成文件,在DOM中增加一個a標籤,href設置成轉出文件地址,手動觸發a標籤的click事件。
downloadFile(data) {
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", "check.xlsx");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
ok,成功下載了.xlsx文件。
問題2:下載的.xlsx文件無法打開,office excel 提示文件被損壞
解決方法:
採用原生的Request對象發送請求,拿到返回的Response,再調用 downloadFile() 方法
var formData = new FormData();//FormData對象用以將數據編譯成鍵值對,以便用XMLHttpRequest來發送數據
formData.append("file", this.$refs.upload.uploadFiles[0].raw);//拿到組件中的上傳文件的原始數據,添加到FormData中
var xhr = new XMLHttpRequest();
xhr.responseType = "blob";//設置responseType 爲 blob
xhr.open(
"POST",
"http://devserver.6tiantian.com/ajax/question-bank-service/admin/question/knowledge_tag/check",
true
);
xhr.onload = function() {
this.downloadFile(xhr.response);
}.bind(this);
xhr.send(formData); //發送請求
},
原因分析:Uploader組件包裝了上傳的文件/請求,導致返回的數據類型出現錯誤,無法轉換成真正的.xlsx文件。
經試驗,用 uploader 自帶的 this.$refs.upload.submit() 發送請求,用 on-success(response,file,fileList)鉤子獲取到的response類型爲string,而 XMLHttpRequest 發送的請求得到的response類型爲 object。
art.js/