createReadStream doesn't take blob url?

ε祈祈猫儿з 提交于 2021-02-11 14:48:12

问题


this.state.videoBlob is a blob object. I used URL.createObjectURL to generate a blob URL, and passed it to fs.createReadStream, like below: fs.createReadStream(URL.createObjectURL(this.state.videoBlob)) This blob url looks like: blobURL: blob:http://localhost:3000/dabe5cdd-00cc-408a-9f3d-b0ba5f2b10b3

But I got an error saying: TypeError: fs.createReadStream is not a function

The problem won't exist if I passed some online video URL. So how can I read blob from fs.createReadStream? Thanks!


回答1:


When I look at the code behind fs.createReadStream(), it calls new ReadStream() and passes the path/url to that. On this line of code, it appears that the only type of url that is supported is a file URL. The doc is silent on that topic which is why I went and looked at the code. So, it does not appear to me that fs.createReadStream() supports that type of pseudo-URL.

Since you just want a readstream from that URL and you have the actual URL of the remote resource, I would suggest you just use either http.get() or request() or something similar as those will all contact the remote host and return to you a readStream. Since your objective was to get a readStream, this is one way to achieve that.

http.get('http://localhost:3000/dabe5cdd-00cc-408a-9f3d-b0ba5f2b10b3', (res) => {
     // res is a readstream here
}).on('error', (err) => {
     // error on the request here
});

FYI, you may find this answer on Blob URLs to be useful. I don't see any evidence that fs.createReadStream() supports blob URLs. In the browser, they are something created only by the internals of the browser and are only useful within that specific web page context (they refer indirectly to some internal storage) and can't be passed outside the web page or even preserved from one web page to the next. If you wanted your server to have access to the actual data from a blob URL created in the browser, you'd have to upload the actual data to your server. Your server can't access a blob URL created in the browser.



来源:https://stackoverflow.com/questions/60858364/createreadstream-doesnt-take-blob-url

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