Node.js 环回-使用远程方法下载pdf

Node.js 环回-使用远程方法下载pdf,node.js,pdf,loopbackjs,strongloop,loopback,Node.js,Pdf,Loopbackjs,Strongloop,Loopback,我想使用远程方法从第三方API下载pdf文件,我的实现是: Staff.statement = async (id, year, month) => { const fileData = await ThirdPartyAPI.restGET(id, year, month); const filename = `Statement_${month}${year}.pdf`; return [fileData, 'application/pdf', `inline;filena

我想使用远程方法从第三方API下载pdf文件,我的实现是:

Staff.statement = async (id, year, month) => {
  const fileData = await ThirdPartyAPI.restGET(id, year, month);
  const filename = `Statement_${month}${year}.pdf`;
  return [fileData, 'application/pdf', `inline;filename=${filename}`];
};

Staff.remoteMethod('statement ', {
  accepts: [
    { arg: 'id', type: 'any', required: true },
    { arg: 'year', type: 'string', required: true },
    { arg: 'month', type: 'string', required: true }
  ],
  description: 'Download statement file',
  http: { path: '/:id/statement', verb: 'GET' },
  returns: [
    { arg: 'body', type: 'file', root: true },
    { arg: 'Content-Type', type: 'string', http: { target: 'header' } },
    { arg: 'Content-Disposition', type: 'string', http: { target: 'header' } }
});
问题是该文件获得了正确的查看/下载(如果我设置了
Content-Disposition:attachment;filename=${filename}
),但它是一个空白的pdf:

我检查了来自swagger的响应数据,数据与我直接从第三方API获得的数据相同:

这是响应标题:

{
  "strict-transport-security": "max-age=0; includeSubDomains",
  "content-encoding": "",
  "x-content-type-options": "nosniff",
  "date": "Mon, 16 Jul 2018 04:50:36 GMT",
  "x-download-options": "noopen",
  "x-frame-options": "SAMEORIGIN",
  "content-type": "application/pdf",
  "transfer-encoding": "chunked",
  "content-disposition": "inline;filename=Statement_062016.pdf",
  "connection": "keep-alive",
  "access-control-allow-credentials": "true",
  "vary": "Origin",
  "x-xss-protection": "1; mode=block"
}
显示器或数据有什么问题

更新:在穆罕默德·拉希姆的帮助下我的实现


您可以在这里查看我下载pdf文件的示例代码

    var request = require('request');
    var fs = require("fs");

var _downloadPdf = function (url,callback) {
    var url = 'your-url';
    var options = {
        method: 'GET',
        url: url,
        headers: {
            'Content-Type': 'application/pdf',
        }
    };
    var _filepath = 'D://Node/upload/temp/sample.pdf';
    //If you want to go with pipe in case of chunk data
    var dest = fs.createWriteStream(filepath);
    request(options, function (error, response, body) {
        if (error)
            throw new Error(error);

    }).on('end', function () {
        return callback(filepath);
    }).on('error', function (err) {
        return callback(err);
    }).pipe(dest);
} 

NodeJs不直接支持原始二进制数据,请使用缓冲区下载二进制数据。@MohammadRaheem我如何才能这样做?我如何才能回调pdf文件而不是“成功”?我尝试了您的方法并检查了文件夹中
sample.pdf
文件的内容,但它仍然为空:-/
    var request = require('request');
    var fs = require("fs");

var _downloadPdf = function (url,callback) {
    var url = 'your-url';
    var options = {
        method: 'GET',
        url: url,
        headers: {
            'Content-Type': 'application/pdf',
        }
    };
    var _filepath = 'D://Node/upload/temp/sample.pdf';
    //If you want to go with pipe in case of chunk data
    var dest = fs.createWriteStream(filepath);
    request(options, function (error, response, body) {
        if (error)
            throw new Error(error);

    }).on('end', function () {
        return callback(filepath);
    }).on('error', function (err) {
        return callback(err);
    }).pipe(dest);
}