简体   繁体   English

如何从客户端上传基于 URL 的 mp3 文件到节点服务器

[英]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.我正在尝试使用 fetch 将 mp3 文件上传到节点服务器。

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?如果是这样,我如何接收它并将文件作为 mp3 写入节点服务器?

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.相反,我认为最好的选择是将 mp3 文件的 url 发送到节点后端,然后使用像 axios 这样的库来下载 mp3 文件并以某种方式保留它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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