Javascript 使用远程方法下载pdf
我打算使用远程方法下载动态生成的pdf文件,该文件存在于特定路径,我使用的是返回类型“file”。我的实施是:Javascript 使用远程方法下载pdf,javascript,node.js,pdf,loopbackjs,strongloop,Javascript,Node.js,Pdf,Loopbackjs,Strongloop,我打算使用远程方法下载动态生成的pdf文件,该文件存在于特定路径,我使用的是返回类型“file”。我的实施是: customer.downloadFile = function downloadFile(userId, res, cb){ var reader = fs.createReadStream(__dirname + '/../document.pdf'); cb(null, reader, 'application/pdf'); }; customer.remote
customer.downloadFile = function downloadFile(userId, res, cb){
var reader = fs.createReadStream(__dirname + '/../document.pdf');
cb(null, reader, 'application/pdf');
};
customer.remoteMethod(
'downloadFile',
{
isStatic: true,
accepts: [
{ arg: 'id', type: 'string', required: true },
{ arg: 'res', type: 'object', 'http': { source: 'res' } }
],
returns: [
{ arg: 'body', type: 'file', root: true },
{ arg: 'Content-Type', type: 'string', http: { target: 'header' } }
],
http: {path: '/:id/downloadFile', verb: 'get'}
}
);
上述代码的问题在于,浏览器虽然显示了漂亮的pdf文件容器,但没有显示文件,而是显示了以下错误:
请指出代码的错误以及如何更正。
从该URL获得线索:您应该使用它来管理可下载的文件
文件分组在所谓的容器中(基本上是一个具有单一层次结构的文件夹,而不是更多层次结构)
如何做到:
container
的模型存储
数据源,用作连接器
环回组件存储容器
建模数据源存储
使用容器,您可以将文件存储到本地文件系统,或稍后移动到云解决方案。通过以下方式实现了这一点:
fs.readFile(fileName, function (err, fileData) {
res.contentType("application/pdf");
res.status(200).send(fileData);
if (!err) {
fs.unlink(fileName);
}
else {
cb(err);
}
});
内容配置:内联;filename=“filename.pdf”//goes-in-headerwhere是
res
定义的?@user3568719很抱歉回答得太晚,我已经修改了这个问题以包含res支持