简体   繁体   English

如何将 HTMLAudioElement 音频文件上传到节点服务器?

[英]How to upload HTMLAudioElement audio file to node server?

I'm currently playing an audio file using an external URL:我目前正在使用外部 URL 播放音频文件:

const url = 'http://s5.qhres.com/static/465f1f953f1e6ff2.mp3';
const audio = new Audio(url);
audio.play();

How can I programmatically upload this file to the node server?如何以编程方式将此文件上传到节点服务器? (so the mp3 file can be stored in the server) (因此mp3文件可以存储在服务器中)

Should I get the audio data and send it to the server using fetch() ?我应该获取音频数据并使用fetch()将其发送到服务器吗?

Some guidance would be appreciated.一些指导将不胜感激。

This is how you can implement an upload function to upload your audioFile这是您如何实现上传功能来上传您的音频文件

  • first we download the file turn it into binary首先我们下载文件把它变成二进制文件
  • second we uploaded第二次我们上传

For the backend in nodejs you can use multer with express to setup and upload API so you can receive the file this is a guide that can help you对于 nodejs 中的后端,您可以使用multer和 express 来设置和上传 API,以便您可以接收文件,这是一个可以帮助您的指南

TBN : Read the code comment and you will understand how it works TBN:阅读代码注释,您将了解它是如何工作的

 // load status span let Status = document.getElementById("status"); // messages for status const DOWN_START = "File is being downloaded ..."; const DOWN_SUCCESS = "File is downloaded ..."; const DOWN_FAIL = "File failed to download with : "; const UPLOAD_START = "File is being Uploaded "; const UPLOAD_SUCCESS = "File is Uploaded"; const UPLOAD_FAILD = "File failed to upload with : "; // function to update status span const updateStatusMsg = (msg) => { Status.innerHTML = msg; }; // instansiate XMLHttpRequest let downloadReq = new XMLHttpRequest(); let fileUrl = null; // XMLHttpRequest Progress Listeners downloadReq.addEventListener("progress", () => updateStatusMsg(DOWN_START)); downloadReq.addEventListener("load", () => updateStatusMsg(DOWN_SUCCESS)); downloadReq.addEventListener("error", () => updateStatusMsg(DOWN_FAIL)); // Grab url from input const getUrl = (self) => { fileUrl = self.value; }; // Download File const getFile = () => { // [TIP] here i added a proxy for the app so you can have a valid cors so it can work locally downloadReq.open( "GET", "https://api.allorigins.win/get?url=" + encodeURIComponent(fileUrl), true ); downloadReq.responseType = "blob"; // Setup listener onLoad downloadReq.onload = function () { //When the file is downloaded we pass it to the upload function uploadFile(downloadReq.response); // if you want also to read the file and play it you can use this let reader = new FileReader(); reader.readAsDataURL(downloadReq.response); reader.onload = function (e) { console.log(reader); console.log("DataURL:", e.target.result); }; }; // Start Request downloadReq.send(); }; // Upload const uploadFile = (blob) => { // Create A file let audioFile = new File([blob], "audioFile"); updateStatusMsg(UPLOAD_START); // Sending Using fetch here you can add your node.js End point fetch("http://www.example.net", { method: "POST", headers: { "Content-Type": "Your Content Type", }, body: audioFile, }) .then((response) => response.json()) .then((success) => updateStatusMsg(UPLOAD_SUCCESS)) .catch((error) => updateStatusMsg(UPLOAD_FAILD + error.message)); };
 <input type="text" placeholder="Enter the file link" onchange="getUrl(this)" /> <input type="button" value="Upload File" onClick="getFile()" /> <br /> <span id="status"></span>

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

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