node.js - Loopback - 使用远程方法下载 pdf
问题描述
我想使用远程方法从第三方 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:
我检查了来自招摇的响应数据,数据与我直接从第三方 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"
}
显示或数据有什么问题?
更新:在 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);
}
推荐阅读
- kubernetes - Falco 审核规则未显示任何警报
- jenkins - Jenkins 共享库 | 抽象一个阶段
- android - React Native:Android 原生模块:DatePickerDialog
- javascript - 关于 Redux Saga 的基本问题:“yield call(...)”是否阻塞?
- ruby-on-rails - Dockerize Rails:你没有写权限
- deep-learning - PyTorch 教程在训练时期使用测试数据集
- unix - 如何转换 - DOUBLE LOW-9 QUOTATION MARK „ 使用 SED 命令
- swiftui - 我们如何向手表应用程序添加启动屏幕?
- c# - 即使抛出异常,Unity 单元测试也会成功
- angular - 无法使用 ng 命令以角度添加 ng-bootstrap