FileWriter API:使用blob写入数据
我有一个类似 blob:blahblah
的 blob url 指向一个文件。我想将此 blob 后面的文件写入本地文件系统。 writer.write() 文档表示它接受一个文件对象(来自输入类型文件)和一个 blob。但尝试此操作时它会引发类型不匹配错误,
fileEntry.createWriter(function(writer) {
writer.write(blob); //blob is a var with the value set to the blob url
我知道问题是 blob
未被接受,但我想知道如何将 blob 存储到文件系统。我之前在脚本中从输入类型文件创建了上述 blob,并将其值存储在 var 中。
编辑
好的,所以我想我应该首先给出更多的代码。
首先,我创建一个 blob url 并将其存储在像这样的 var 中
files[i]['blob'] = window.webkitURL.createObjectURL(files[i]);
files
来自输入类型文件 html 标记,并且循环获取文件数量。你知道演出。
然后变量通过多种媒介,首先通过 chrome 的消息传递 api 到另一个页面,然后通过 postMessage
从该页面到工作人员,最后通过 postMessage< 返回到父页面/代码> 再次。
在最后一页上,我打算使用它通过文件系统 api 将 blob 的文件存储到本地文件系统,如下所示。
//loop code
fileSystem.root.getFile(files[i]['name'], {create: true}, function(fileEntry) {
fileEntry.createWriter(function(writer) {
writer.write(files[i]['blob']);
});
});
//loop code
但是 writer.write
抛出 Uncaught Error: TYPE_MISMATCH_ERR: DOM File Exception 11
我相信这个错误是因为提供给 writer.write
的变量是一个文本,而不是来自 createObjectUrl
之类的 blob 对象(直接和不是在传递多个页面/范围之后)或者不是 window.WebKitBlobBuilder
。那么如何使用 blob 的 url 来存储文件呢?
i have a blob url like blob:blahblah
that points to a file. I want to write the file behind this blob to local filesystem. The writer.write()
documentation says it accepts a file object (from input-type-file) and a blob. But it throws a type mismatch error when try this
fileEntry.createWriter(function(writer) {
writer.write(blob); //blob is a var with the value set to the blob url
i know the problem is that the blob
does not get accepted but i would like to know how can i store a blob to the filesystem. i created the said blob earlier in the script from input-type-file and stored it's value in a var.
EDIT
Ok so i think i should have given more code in the first place.
first i create a blob url and store it in a var like this
files[i]['blob'] = window.webkitURL.createObjectURL(files[i]);
files
is from an input-type-file html tag and i is looped for number of files. you know the gig.
then the variable goes through a number of mediums, first through chrome's message passing api to another page and then from that page to a worker via postMessage
and then finally back to the parent page via postMessage
again.
on the final page i intend to use it to store the blob's file to local file system via file system api like this..
//loop code
fileSystem.root.getFile(files[i]['name'], {create: true}, function(fileEntry) {
fileEntry.createWriter(function(writer) {
writer.write(files[i]['blob']);
});
});
//loop code
but the writer.write
throws Uncaught Error: TYPE_MISMATCH_ERR: DOM File Exception 11
i believe this error is because the variable supplied to writer.write
is a text and not a blob object from something like createObjectUrl
(directly and not after passing through multiple pages/scopes) or not a window.WebKitBlobBuilder
. So how can a blob's url be used to store a file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您编辑的代码片段和描述来看,听起来您正在将 blobURL 写入文件系统而不是
File
本身(例如files[i]['name']
是一个 URL)。相反,在主页 -> 之间传递File
对象。其他页面->工人->主页。截至最近(至少在 Chrome 中),您的往返现已成为可能。File
对象可以传递给window.postMessage()
,而在此之前,浏览器将参数序列化为字符串。您可以使用
createObjectURL()
“塑造”对Blob
的处理程序/引用。确实没有办法从 blobURL 返回到Blob
。简而言之,无需创建createObjectURL()
。只需直接传递files[i]
即可。From your edited code snippet and description, it sounds like you're writing the blobURL to the filesystem rather than the
File
itself (e.g.files[i]['name']
is a URL). Instead, pass around theFile
object between main page -> other page -> worker -> main page. As of recent (in Chrome at least), your round trip is now possible.File
objects can be passed towindow.postMessage()
, whereas before, the browser serialized the argument into a string.You 'fashion' a handler/reference to a
Blob
withcreateObjectURL()
. There's not really a way to go from blobURL back to aBlob
. So in short, no need to createcreateObjectURL()
. Just pass aroundfiles[i]
directly.