Node.js Download File Using Content Disposition as Filename

前端 未结 3 985
栀梦
栀梦 2021-02-01 17:22

I\'m using the Request module to download files, but I\'m not quite sure how to pipe the response to an output stream when the filename must come from the \'Content-Disposition\

3条回答
  •  借酒劲吻你
    2021-02-01 17:50

    I'm reqesting a image from yahoo and it isn't using the content-disposition header but I am extracting the date and content-type headers to construct a filename. This seems close enough to what you're trying to do...

    var request = require('request'),
    fs = require('fs');
    
    var url2 = 'http://l4.yimg.com/nn/fp/rsz/112113/images/smush/aaroncarter_635x250_1385060042.jpg';
    
    var r = request(url2);
    
    r.on('response',  function (res) {
      res.pipe(fs.createWriteStream('./' + res.headers.date + '.' + res.headers['content-type'].split('/')[1]));
    
    });
    

    Ignore my image choice please :)

提交回复
热议问题