How to download a file with Node.js (without using third-party libraries)?

后端 未结 28 1240
逝去的感伤
逝去的感伤 2020-11-22 03:37

How do I download a file with Node.js without using third-party libraries?

I don\'t need anything special. I only want to download a file from a giv

相关标签:
28条回答
  • 2020-11-22 04:04

    gfxmonk's answer has a very tight data race between the callback and the file.close() completing. file.close() actually takes a callback that is called when the close has completed. Otherwise, immediate uses of the file may fail (very rarely!).

    A complete solution is:

    var http = require('http');
    var fs = require('fs');
    
    var download = function(url, dest, cb) {
      var file = fs.createWriteStream(dest);
      var request = http.get(url, function(response) {
        response.pipe(file);
        file.on('finish', function() {
          file.close(cb);  // close() is async, call cb after close completes.
        });
      });
    }
    

    Without waiting for the finish event, naive scripts may end up with an incomplete file. Without scheduling the cb callback via close, you may get a race between accessing the file and the file actually being ready.

    0 讨论(0)
  • 2020-11-22 04:05

    I prefer request() because you can use both http and https with it.

    request('http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg')
      .pipe(fs.createWriteStream('cat.jpg'))
    
    0 讨论(0)
  • 2020-11-22 04:05

    If you are using express use res.download() method. otherwise fs module use.

    app.get('/read-android', function(req, res) {
       var file = "/home/sony/Documents/docs/Android.apk";
        res.download(file) 
    }); 
    

    (or)

       function readApp(req,res) {
          var file = req.fileName,
              filePath = "/home/sony/Documents/docs/";
          fs.exists(filePath, function(exists){
              if (exists) {     
                res.writeHead(200, {
                  "Content-Type": "application/octet-stream",
                  "Content-Disposition" : "attachment; filename=" + file});
                fs.createReadStream(filePath + file).pipe(res);
              } else {
                res.writeHead(400, {"Content-Type": "text/plain"});
                res.end("ERROR File does NOT Exists.ipa");
              }
            });  
        }
    
    0 讨论(0)
  • 2020-11-22 04:08

    Path : img type : jpg random uniqid

        function resim(url) {
    
        var http = require("http");
        var fs = require("fs");
        var sayi = Math.floor(Math.random()*10000000000);
        var uzanti = ".jpg";
        var file = fs.createWriteStream("img/"+sayi+uzanti);
        var request = http.get(url, function(response) {
      response.pipe(file);
    });
    
            return sayi+uzanti;
    }
    
    0 讨论(0)
  • 2020-11-22 04:08
    var requestModule=require("request");
    
    requestModule(filePath).pipe(fs.createWriteStream('abc.zip'));
    
    0 讨论(0)
  • 2020-11-22 04:11
    var fs = require('fs'),
        request = require('request');
    
    var download = function(uri, filename, callback){
        request.head(uri, function(err, res, body){
        console.log('content-type:', res.headers['content-type']);
        console.log('content-length:', res.headers['content-length']);
        request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
    
        }); 
    };   
    
    download('https://www.cryptocompare.com/media/19684/doge.png', 'icons/taskks12.png', function(){
        console.log('done');
    });
    
    0 讨论(0)
提交回复
热议问题