简体   繁体   English

如果我得到应用程序流的 Axios 响应内容类型,如何保存 PDF 格式文件?

[英]How to save PDF format file if I get the Axios response content-type of application-stream?

I would like to save a octet-stream type data to a pdf file, the data is correct as I have tried to use Postman "Send and Save" function to get and open the pdf file successfully.我想将一个八位字节流类型的数据保存到一个 pdf 文件中,数据是正确的,因为我试图使用 Postman 的“发送和保存”功能来成功获取和打开 pdf 文件。

However, once I call axios request and receive the response from the server, I cannot get the correct pdf file.但是,一旦我调用 axios 请求并收到服务器的响应,就无法获得正确的 pdf 文件。 After I got the pdf file, it seems that the file is broken, can I cannot open it correctly.拿到pdf文件后,好像文件坏了,我能不能正确打开它。

Here's the header of the response: enter image description here这是响应的标题:在此处输入图像描述

        axios.create({
        baseURL: link,
        timeout: 60000,
        headers: {
            Authorization: token
        }
    }).post(apiUrl, {
        .
        .
        .
    }).then(res=>{
        var file_data = res["data"];
        var file_type = res["headers"]["content-type"];
        var blob = new Blob([file_data], {type: file_type});
        saveAs(blob, "Test.pdf")
    });

I found the solution for this problem, it seems that the size of the file is different with the original one, and I make the following changes to receive the data in form of Blob object to solve it.我找到了这个问题的解决方案,看起来文件的大小与原来的不同,我做了以下更改以接收Blob对象形式的数据来解决它。

axios.create({
    baseURL: link,
    timeout: 60000,
    responseType: 'blob'
    headers: {
        Authorization: token
    }
}).post(apiUrl, {
    .
    .
    .
}).then(res=>{
    var file_data = res["data"];
    var file_type = res["headers"]["content-type"];
    saveAs(file_data, "Test.pdf");
});

When downloading binaries with Axios, the format of the data returned by the server needs to be specified with the request.使用axios下载二进制文件时,需要在请求中指定服务器返回的数据格式。 In the case of a PDF file, generally "arraybuffer" needs to be used.对于 PDF 文件,通常需要使用“arraybuffer”。

axios.get(
      "https://example.com/example.pdf",
      {
        responseType: "arraybuffer"
      }
    );

As per the documentation, the valid options are:根据文档,有效的选项是:

// `responseType` indicates the type of data that the server will respond with
// options are: 'arraybuffer', 'document', 'json', 'text', 'stream'
//   browser only: 'blob'
//   default: 'json'

Axios documentation Axios 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在文本文件中保存响应类型“ Content-Type:text / javascript” - Save response type “Content-Type : text/javascript” in text file 如何使用 Fetch 从响应标头中获取内容类型 - How to get content-type from the response headers with Fetch 反应,axios,内容类型 - React, axios, content-type HTML文件上传:有没有办法强制content-type =“application / octet-stream” - HTML file upload: is there a way to force content-type=“application/octet-stream” Axios 不尊重 Content-Type 标头 - Axios not respecting Content-Type header Header axios 中定义的内容类型不起作用 - Header Content-Type defined in axios not working 从XHR获取响应Content-Type标头 - Get the response Content-Type header from XHR file_get_contents():假设应用程序/x-www-form-urlencoded 未指定内容类型 - file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded “axios.defaults.headers.common['Content-Type'] = 'application/json'” 但 axios.post 仍然是“application/x-www-form-urlencoded” - "axios.defaults.headers.common['Content-Type'] = 'application/json'" but axios.post still is "application/x-www-form-urlencoded" 如何在 React Native 中知道 API 响应的 Content-Type - How to know the Content-Type of the API response in react native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM