简体   繁体   English

Javascript 如何从链接上传 mp3 文件

[英]Javascript how to upload a mp3 file from a link

I'm wanting to get the contents of a mp3 file from a url get_mp3_file(mp3_file) , then upload the contents of the mp3 I just downloaded, to another webserver and get the response.我想从 url get_mp3_file(mp3_file)获取 mp3 文件的内容,然后将我刚刚下载的 mp3 的内容上传到另一个网络服务器并获得响应。

var mp3_url = 'https://example.com/song.mp3';

function get_mp3_file(mp3_url) {
    const xhr = new XMLHttpRequest();
    xhr.open('GET', mp3_url, false);
    xhr.send();
    return xhr.responseText;
}

var fd = new FormData();
fd.append("file", get_mp3_file(mp3_url));

var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://mp3-to-wav.com/upload-file.php', false);
xhr.send(fd);
var wav_link = JSON.parse(xhr.responseText).link;

the key was the File class.关键是文件 class。 here's more about it.这里有更多关于它的信息。 https://w3c.github.io/FileAPI/#file-constructor https://w3c.github.io/FileAPI/#file-constructor

The solution for retrieving the contents of a file from one webserver, and then uploading it to another webserver:从一个网络服务器检索文件内容,然后将其上传到另一个网络服务器的解决方案:

var mp3_url = 'https://example.com/song.mp3';

var xhr = new XMLHttpRequest();
xhr.open('GET', mp3_url, false);
xhr.responseType = 'blob';

xhr.onload = function() {
    var xhr2 = new XMLHttpRequest();

    var file = new File([xhr.response], 'song.mp3', {type: 'audio/mp3'});
    var formData = new FormData();
    formData.append('file', file);
    

    xhr2.open('POST', "https://convert-mp3-to-wav.com", false);
    xhr2.send(formData);

}

xhr.send();

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

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