用input的file类型调用手机录音功能,请问如何把手机里的录音上传至服务器

<input type="file" id="ids" accept="audio/*" capture="microphone" @change="preview($event)"/>
这是用input调用手机录音功能的,
成功后获取到的路径是:blob:file:///2211e58e-4cc5-49d0-900f-fa129a57dd50
请问这种格式的录音路径需要转换吗?求高手帮忙看一下,感激不尽 谢谢

阅读 3.1k
2 个回答

参见如下内容。

var xhr = new XMLHttpRequest();
xhr.open('GET', 'blob:http%3A//your.blob.url.here', true);
xhr.responseType = 'blob';
xhr.onload = function(e) {
  if (this.status == 200) {
    var myBlob = this.response;
    // 在这里,你可以使用 FromData 对象再次调用 ajax 将 blob 的内容以二进制发送到你的服务器

  }
};
xhr.send();

使用FormDta

function preview(e) {
    if(e.target.files.length === 0) {
        alert('未选择文件');
        return;
    }
    const file = e.target.files[0];
    const xhr = new XMLHttpRequest();
    xhr.open('POST','http://example.com/upload',true);
    xhr.onload = function() {
        // 上传完毕
        console.log(xhr.responseText)
    }
    xhr.onerror = function(e) {
        console.error(e);
    }
    const fd = new FormData();
    fd.append("file",file);
    xhr.send(fd)
}
推荐问题