Loopback - 使用远程方法下载 pdf

Loopback - download pdf using remote method

我想使用远程方法从第三方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' } }
});

问题是文件 view/downloads 正确(如果我设置 Content-Disposition: attachment;filename=${filename})但它是一个空白的 pdf:

我查看了swagger的响应数据,和我直接从第三方获取的数据是一样的API:

这是回复headers:

{
  "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"
}

显示或数据有什么问题?

Updates: My implementation after the help of Mohammad Raheem

Staff.statement = (id, year, month, res, cb) => {
    const options = {
      url: // {{request_url}},
      headers: {
        authorization: // {{token}}
      }
    }; 

    request(options)
      .on('error', err => cb(err))
      .on('response', response => cb(null, response, 'application/pdf'))
      .pipe(res);

    // using 'request-promise' package:
    // request(options)
    //   .then(response => cb(null, response, 'application/pdf'))
    //   .catch(err => cb(err));
  };
};

Staff.remoteMethod('statement ', {
  accepts: [
    { arg: 'id', type: 'any', required: true },
    { arg: 'year', type: 'string', required: true },
    { arg: 'month', type: 'string', required: true },
    { arg: 'res', type: 'object', http: { source: 'res' } }
  ],
  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' } }
});

你可以在这里查看我做了下载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);
}