How to save blob url data in a directory

狂风中的少年 提交于 2019-12-05 06:46:41

问题


I am working on the audio record. Now I want to save that recorded audio in our local directly. When I record audio it is returning blob URL like:

blob: http://example.com/7737-4454545-545445.

When I hit this URL it plays recorded audio. Now I am sending that blob URL from js to PHP script to save your local directory but it is not working.

How can I do that?


回答1:


Blob URL lifetime is linked to document which created Blob URL from Blob or File object. Blob URL cannot be posted to server as reference for a Blob stored at snapshot state. See Blob URL Store.

You can use fetch(), Response.blob() to get Blob representation of Blob URL, post FormData to server.

// `blobURL` : `"blob:http://example.com/7737-4454545-545445"`
fetch(blobUrl).then(response => response.blob())
.then(blob => { 
  const fd = new FormData();
  fd.append("fileName", blob, "file.ext"); // where `.ext` matches file `MIME` type  
  return fetch("/path/to/server", {method:"POST", body:fd})
})
.then(response => response.ok)
.then(res => console.log(res))
.catch(err => console.log(err));


来源:https://stackoverflow.com/questions/43730082/how-to-save-blob-url-data-in-a-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!