代码之家  ›  专栏  ›  技术社区  ›  Jee Mok ecanf

环回-使用远程方法下载PDF

  •  1
  • Jee Mok ecanf  · 技术社区  · 8 年前

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

    staff.statement=async(id,year,month)=>。{
    const filedata=等待thirdpartyapi.restget(id,year,month);
    const filename=`statement month$year.pdf`;
    返回[filedata,'application/pdf',`inline;filename=$文件名`];
    };
    
    staff.remotemethod('语句',){
    接受:[
    arg:'id',type:'any',required:true,
    arg:'year',type:'string',required:true,
    arg:'month',type:'string',required:true
    ,
    description:'下载语句文件',
    http:路径:“/:id/statement”,动词:“get”,
    返回:[
    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”,
    “内容编码”:“”,
    “x-content-type-options”:“nosniff”,
    “日期”:“周一,2018年7月16日04:50:36 GMT”,
    “x-download-options”:“noopen”,
    “x-frame-options”:“sameorigin”,
    “content type”:“应用程序/pdf”,
    “传输编码”:“分块”,
    “content disposition”:“inline;filename=statement_.pdf”,
    “connection”:“保持活动”,
    “访问控制允许凭据”:“真”,
    “vary”:“来源”,
    “x-xss-protection”:“1;模式=块”
    }
    

    显示器或数据有什么问题?

    更新:my implementation after the help of mohammad raheem

    staff.statement=(id,year,month,res,cb)=>。{
    常量选项={
    网址:/请求,
    标题:{
    授权:/代币
    }
    };
    
    请求(选项)
    .on(“错误”,err=>cb(err))
    .on('response',response=>cb(空,response,'application/pdf'))
    .管道(Res);
    
    //使用“请求承诺”包:
    //请求(选项)
    //.then(response=>cb(空,response,'application/pdf'))
    //.catch(err=>cb(err));
    };
    };
    
    staff.remotemethod('语句',){
    接受:[

    Content-Disposition: attachment;filename=${filename}

    enter image description here

    enter image description here

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

    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' } }
    });
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   Mohammad Raheem    8 年前

        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);
    }