[英]Javascript - writing image file to blob (html5 file api)
I'm working with a chrome extension to copy user selected files into the extension's filesystem. 我正在使用chrome扩展程序,将用户选择的文件复制到扩展程序的文件系统中。 I'm not getting any errors, but when I try to view the image, it's broken.
我没有收到任何错误,但是当我尝试查看图像时,它坏了。 Here's an example of the code:
这是代码示例:
this.create = function(obj, attr, calling_model){
// Get parent directory
fs.root.getDirectory(obj.type, {create: true}, function(dirEntry) {
// Create file
dirEntry.getFile(obj.type+'-'+obj.id+'-'+attr.name, {create: true, exclusive: true}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.write(attr.value);
}, errorHandler);
alert('just wrote a file to: '+fileEntry.toURL());
// Update passed object
obj[attr.name] = fileEntry.toURL();
calling_model.update(obj);
}, errorHandler);
}, errorHandler);
};
where attr.value = the value of a file input. 其中attr.value =文件输入的值。 I feel like I should be turning the value of the file input into a blob before writing it to the filesystem, but I haven't found any examples of how to do that.
我觉得我应该先将文件输入的值转换为Blob,然后再将其写入文件系统,但是我还没有找到任何执行此操作的示例。 Has anyone tackled this problem before?
有人解决过这个问题吗?
Thanks in advance... 提前致谢...
If attr.value
is a File
object, your code should work. 如果
attr.value
是File
对象,则您的代码应该可以工作。 I wrote a section on "duplicating user selected files" in my HTML5Rocks article on the Filesystem API that should help. 我在文件系统API的HTML5Rocks文章中写了关于“复制用户选择的文件”的部分,该部分应该会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.