我收到了从 VueJS 前端到后端服务器的请求:
let config = {header : {'Content-Type' : 'multipart/form-data'}};
const res = await axios.patch(`http://0.0.0.0:8080/report/${this.docGuid}`, data, config);
响应头:
HTTP/1.1 200 OK
date: Tue, 30 Mar 2021 13:05:00 GMT
server: uvicorn
content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
content-disposition: attachment; filename*=utf-8''IL-NS-0_LSM.docx
content-length: 600357
last-modified: Tue, 30 Mar 2021 13:05:01 GMT
etag: c1a4f72bf0ad07234099ac6cec97e635
请求头:
PATCH /report/cb07f12a7a49435e8e0f306e71c01969 HTTP/1.1
Host: 0.0.0.0:8080
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:87.0) Gecko/20100101 Firefox/87.0
Accept: application/json, text/plain, */*
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=---------------------------129404071228287858071180535042
Content-Length: 95039
Connection: keep-alive
它正在服务器端创建一个 MS docx 文件,并在响应中恢复了这个创建的文件。 我尝试下载它:
let blob = new Blob([res.data], {type: res.headers["content-type"]});
window.open(URL.createObjectURL(blob));
它可以工作,除了下载的文件已损坏,我无法打开它。除了服务器端副本很好并且可以打开。
MIME 类型设置为 "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
可能有什么问题? 提前致谢)
答案 0 :(得分:1)
我终于发现应该有responseType
参数设置为blob
。
所以:
config = {header : {'Content-Type' : 'multipart/form-data'}, responseType: 'blob'};