简体   繁体   中英

Downloading a mp3 file from another server to my server

I have a nodejs server where I use a GET request to receive the contents of a mp3 file. Then I use writefile to copy the contents into a mp3 file. The problem is that the mp3 file is broken or something because I can't play it with any mp3 player, but writefile worked successfully.

 request(options, function(error,response,body) {

    var path = "C://Users/foo/pop.mp3";
    var wstream = fs.createWriteStream(path);
    wstream.write(body);

    wstream.end();


    res.status(200).send(body);
  });

Your body parameter doesn't just contain the straight body, so when you're calling wstream.write(body) you're just writing the body object and not the actual data.

Make the following change

wstream.write(body.Body.data)

我通过将响应直接传递到文件并将请求的内容类型标头设置为ISO-8859-1来解决了该问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM