Javascript-将图像文件写入blob(html5文件api)

Javascript - writing image file to blob (html5 file api)

本文关键字:文件 html5 api blob 图像 Javascript-      更新时间:2023-09-26

我正在使用chrome扩展将用户选择的文件复制到扩展的文件系统中。我没有任何错误,但当我试图查看图像时,它已损坏。下面是一个代码示例:

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

其中attr.value=文件输入的值。我觉得我应该在将文件输入的值写入文件系统之前将其转换为blob,但我还没有找到任何如何做到这一点的示例。以前有人解决过这个问题吗?

提前感谢。。。

如果attr.valueFile对象,那么您的代码应该可以工作。我在关于文件系统API的HTML5Rocks文章中写了一个关于"复制用户选择的文件"的部分,这应该会有所帮助。