最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Downloading Mp3 file from remote. Node js - Stack Overflow

programmeradmin1浏览0评论

I am trying to download a mp3 file from the remote url using node js. For that I am using the following code. But It doesn't work (File that downloading having 0 bytes only its not playing once it downloaded).

var http = require('http');
var fs = require('fs');
var url = ".mp3";
var dest = "2.mp3";
var file = fs.createWriteStream(dest);
var request = http.get(url, function(response) {
    console.log("res "+response);
    response.pipe(file);
    file.on('finish', function() {
        console.log("File download Completed");
    });
}).on('error', function(err) { // Handle errors

});

I am trying to download a mp3 file from the remote url using node js. For that I am using the following code. But It doesn't work (File that downloading having 0 bytes only its not playing once it downloaded).

var http = require('http');
var fs = require('fs');
var url = "http://play.publicradio.org/rivet/d/podcast/marketplace/segments/2015/09/28/mp_20150928_seg_01_64.mp3";
var dest = "2.mp3";
var file = fs.createWriteStream(dest);
var request = http.get(url, function(response) {
    console.log("res "+response);
    response.pipe(file);
    file.on('finish', function() {
        console.log("File download Completed");
    });
}).on('error', function(err) { // Handle errors

});
Share Improve this question asked Sep 29, 2015 at 10:24 Kishore IndragantiKishore Indraganti 1,3223 gold badges18 silver badges34 bronze badges 3
  • 2 Try hacksparrow.com/using-node-js-to-download-files.html – Chetan Commented Sep 29, 2015 at 10:34
  • I had a similar issue. You can check out how I solved it: stackoverflow.com/questions/29987847/… – Pio Commented Sep 29, 2015 at 10:37
  • @Chetan :same problem – Kishore Indraganti Commented Sep 29, 2015 at 10:47
Add a comment  | 

1 Answer 1

Reset to default 16

The problem here is that http doesn't follow redirects. You can use the request npm module that does it by default to avoid handling headers yourself.

var fs = require('fs'),
  request = require('request');

request
  .get('http://foo.com/bar.mp3')
  .on('error', function(err) {
    // handle error
  })
  .pipe(fs.createWriteStream('2.mp3'));
发布评论

评论列表(0)

  1. 暂无评论