简体   繁体   中英

How to upload URL-based mp3 file from the client to the node server

I'm trying to upload a mp3 file to node server using fetch.

Is the below code correct way to do this?

 var song;
  toDataUrl('http://s5.qhres.com/static/465f1f953f1e6ff2.mp3', function(myBase64) {
    // console.log(myBase64); // myBase64 is the base64 string
    song = myBase64;
});

  fetch("/ffmpegserver/upload", {
    method: 'PUT',
    headers: { 'Accept': 'audio/mpeg', 'Content-Type': 'audio/mpeg' },
    body: song
  })
    .then(response => {
      console.log("Got response after uploading song:", response);
    })
    .catch(error => {
      console.log("Error in Firebase AC upload song: ", error);
    });
}

If so, how can I receive it and write the file as mp3 in the node server?

app.put("/ffmpegserver/upload", (req, res) => {
  var mp3SongName = "output/test.mp3";
  var mp3_file = fs.createWriteStream(mp3SongName);
// how to write the mp3 file?
}

This code would need something on the client that would steam the audio into a buffer and then send that buffer to the node server.

Instead of that, I think the best option would be to send the url of the mp3 file to the node backend, and then use a library like axios to download the mp3 file and persist it somehow.

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