繁体   English   中英

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

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

我正在尝试使用 fetch 将 mp3 文件上传到节点服务器。

下面的代码是正确的方法吗?

 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);
    });
}

如果是这样,我如何接收它并将文件作为 mp3 写入节点服务器?

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

此代码需要客户端上的某些内容将音频传输到缓冲区中,然后将该缓冲区发送到节点服务器。

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

暂无
暂无

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

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