Django Vue js下载的excel文件已损坏
我在Vue中下载excel文件时遇到问题。后端中的url看起来正常: “file”参数将我的url存储在对象中 this.scenario.file=“” 以及以下响应对象详细信息:Django Vue js下载的excel文件已损坏,django,vue.js,Django,Vue.js,我在Vue中下载excel文件时遇到问题。后端中的url看起来正常: “file”参数将我的url存储在对象中 this.scenario.file=“” 以及以下响应对象详细信息: myresponce:Object config:Object adapter:ƒ xhrAdapter(config) data:undefined headers:Object Accept:"application/vnd.ms-excel" maxContentLength:-1 method:"get"
myresponce:Object
config:Object
adapter:ƒ xhrAdapter(config)
data:undefined
headers:Object
Accept:"application/vnd.ms-excel"
maxContentLength:-1
method:"get"
responseType:"arraybuffer"
timeout:0
transformRequest:Array[1]
0:ƒ transformRequest(data, headers)
transformResponse:Array[1]
0:ƒ transformResponse(data)
url:"http://127.0.0.1:8000/media/datafiles/
Input_curves_bm7ionE.xlsx"
validateStatus:ƒ validateStatus(status)
xsrfCookieName:"XSRF-TOKEN"
xsrfHeaderName:"X-XSRF-TOKEN"
data:ArrayBuffer
headers:Object
content-length:"1345"
content-type:"text/html; charset=utf-8"
date:"Sun, 07 Jun 2020 22:44:10 GMT"
server:"WSGIServer/0.2 CPython/3.7.7"
vary:"Cookie"
x-content-type-options:"nosniff"
x-frame-options:"DENY"
request:XMLHttpRequest
status:200
statusText:"OK"
你可以试试这样的
axios({
url: this.scenario.file,
method: "GET",
headers: {"Accept": "application/vnd.ms-excel"},
responseType: "arraybuffer"
}).then(response => {
const url = window.URL.createObjectURL(
new Blob([response.data], {type: "application/csv"})
);
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", `file.xlsx`);
document.body.appendChild(link);
link.click();
window.URL.revokeObjectURL(url);
link.remove();
});
仍然存在相同的问题,该文件已下载但已损坏..大小为2kbexcel的文件存储在我的应用程序中,url存储在db中。当尝试手动打开文件时,没问题。@Yauheni你能尝试解压缩文件(将其视为.zip文件)吗?如果可以,这意味着文件没有损坏,但生成文件时出现了问题。不确定你的意思…)我对这类东西有点陌生。我的第一个web应用程序你能检查一下网络响应吗?它给了你什么?
axios({
url: this.scenario.file,
method: "GET",
headers: {"Accept": "application/vnd.ms-excel"},
responseType: "arraybuffer"
}).then(response => {
const url = window.URL.createObjectURL(
new Blob([response.data], {type: "application/csv"})
);
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", `file.xlsx`);
document.body.appendChild(link);
link.click();
window.URL.revokeObjectURL(url);
link.remove();
});