createReadStream不带Blob网址吗? [英] createReadStream doesn't take blob url?

查看:60
本文介绍了createReadStream不带Blob网址吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

this.state.videoBlob 是一个Blob对象.我使用URL.createObjectURL生成Blob URL,并将其传递给fs.createReadStream,如下所示: fs.createReadStream(URL.createObjectURL(this.state.videoBlob))这个Blob网址看起来像: blobURL:blob:http://localhost:3000/dabe5cdd-00cc-408a-9f3d-b0ba5f2b10b3

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

但是我收到一个错误消息: TypeError:fs.​​createReadStream不是函数

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

如果我传递了一些在线视频URL,该问题将不存在.那么如何从 fs.createReadStream 中读取blob?谢谢!

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

推荐答案

当我查看 fs.createReadStream()背后的代码时,它将调用new ReadStream()并将路径/URL传递给那.在这行代码上,看来,唯一受支持的URL类型是文件URL.该文档对该主题保持沉默,这就是为什么我去查看代码.因此,在我看来 fs.createReadStream()不支持这种类型的伪URL.

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.

由于您只需要该URL的读取流,并且拥有远程资源的实际URL,因此建议您只使用 http.get() request()或类似的代码都将与远程主机联系并向您返回readStream.由于您的目标是获取readStream,因此这是实现该目标的一种方法.

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
});

仅供参考,您可能会发现有关Blob URL的答案很有用.我没有任何证据表明 fs.createReadStream()支持Blob URL.在浏览器中,它们仅由浏览器的内部组件创建,并且仅在特定的网页上下文中有用(它们间接引用某些内部存储),并且不能在网页外部传递,甚至不能从一个网站保留翻到下一页.如果您希望服务器可以从在浏览器中创建的Blob URL访问实际数据,则必须将实际数据上传到服务器.您的服务器无法访问在浏览器中创建的Blob URL.

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.

这篇关于createReadStream不带Blob网址吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆