【问题标题】:docx file gets corrupted while downloading from backend serverdocx 文件在从后端服务器下载时损坏
【发布时间】:2021-03-30 13:25:07
【问题描述】:

我收到了一个从 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"

可能出了什么问题? 提前致谢)

【问题讨论】:

    标签: javascript docx


    【解决方案1】:

    我终于发现应该有responseType参数设置为blob。 所以:

    config = {header : {'Content-Type' : 'multipart/form-data'}, responseType: 'blob'};
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 2019-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多