使用nodejs下载应用程序/ pdf

问题描述

am从出站API接收带有以下标头的文件

enter image description here

内容如下所示

enter image description here

有关如何以及无法将此类文件保存在我的临时目录中的某些信息。保存后文件看起来不对。 有一种以正确的方式保存它的方法

 axios.get(process.env.soZLESME_IMZA_API + '/downloadfile?SignerId=' + req.body.SignerId + '&FileKey=' + req.body.FileKey,await getHeaders())
    .then(response => {
 // save here      
 
  //      helper.sendResult(res,response.data,null,req);
    })
    .catch(err => {
        helper.sendResult(res,err,req);
    });

解决方法

您应该使用“ downloadjs”节点模块下载pdf文件。

const download = require('downloadjs');

...

    const specificationId = this.$route.params.specificationId;

    axios
        .get(process.env.SOZLESME_IMZA_API + '/downloadfile?SignerId=' + req.body.SignerId + '&FileKey=' + req.body.FileKey,{
            headers: await getHeaders(),responseType: 'blob',// browser only option
        })
        .then(response => {
           const content = response.headers['content-type'];
           download(response.data,file_name,content);
        })
        .catch(error => console.log(error));

...