从本地文件系统文件条目创建createObjectURL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从本地文件系统文件条目创建createObjectURL相关的知识,希望对你有一定的参考价值。

我的应用程序是一个cordova应用程序。我尝试为平台浏览器执行以下操作。我知道在真实设备平台上不会出现被描述的问题。

我尝试做以下事情:

  1. 通过XMLHttpRequest下载图像文件
  2. 使用来自XMLHttpRequest响应的blob通过createObjectURL显示图像
  3. 将文件写入本地文件系统(使用cordova-plugin-file在Firefox中提供fileSystem API)
  4. 使用本地Filesystem中文件的blob通过createObjectURL显示图像

我可以从保存的文件中创建一个blob,但之后图像就会被破坏。

这是代码:

    var filesystem;
    var downloaddir;
    var filename = 'e44498f0b0964152632bd0c82342914b859c543e.jpeg'
    var downloadurl = 'http://adomain.com/public/content_images/'+filename;


    function download(){
      filesystem.root.getFile(
        '/ressources/'+filename, 
        { create: true, exclusive: false }, 
        function (fileEntry) {  

          var oReq = new XMLHttpRequest();

          oReq.open("GET", downloadurl, true);
          oReq.responseType = "blob";
          oReq.onload = function (oEvent) {
            var blob = oReq.response;
            if (blob) {         
              fileEntry.createWriter(function (fileWriter) {                        
                fileWriter.onwriteend = function (e) {
                  console.log(fileEntry.toURL());// Works in Chrome and Firefox, but file URIs cannot be used for security reasons. So image is not displayed if this RUL is used in image src attribute
                  console.log(window.URL.createObjectURL(blob));// work in chrome and firefox. This is what I like to have in the end but using a file.

                  // Now I want to use the file and transform it in a objectURL, this is where I struggle
                  fileEntry.file(function(file){
                    console.log(window.URL.createObjectURL(file));// works in chrome bot not in firefox. Firefox says :"TypeError: Argument 1 is not valid for any of the 1-argument overloads of URL.createObjectURL."

                    var readerBlob = new FileReader();                              
                    readerBlob.onload = function(event){                                    
                      var blob = new Blob([event.target.result], {type: 'image/jpg'});
                      console.log(window.URL.createObjectURL(blob));// blob uri will be created, but image is broken. Here I want to have a working objectURL that work for Chrome and Firefox           
                    };
                    readerBlob.readAsBinaryString(file);
                  });

                };
                fileWriter.onerror = function (e) {
                };
                fileWriter.write(blob);
              });

            } else console.error('we didnt get an XHR response!');
          };
          oReq.send(null);
        }, 
        function (error) {
          console.log('error creating file');
          console.log(error);
        }
      );
    }

    document.addEventListener("deviceready", function(){
      window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
      window.requestFileSystem(window.TEMPORARY, 10 * 1024*1024, function (fs) {
        filesystem = fs;            
        filesystem.root.getDirectory(
          '/ressources/',
          {create:true}, 
          function(dirEntry){
            console.log('download dir created');
            downloaddir = dirEntry;
            download();
          },
          function(error){
          }
        );
      });
    }, false);
答案

发现错误:我必须使用readAsArrayBuffer读取文件。这就是诀窍。所以在这里工作代码,也许它可以帮助某人:

    var filesystem;
    var downloaddir;
    var filename = 'e44498f0b0964152632bd0c82342914b859c543e.jpeg'
    var downloadurl = 'http://adomain.com/public/content_images/'+filename;


    function download(){
      filesystem.root.getFile(
        '/ressources/'+filename, 
        { create: true, exclusive: false }, 
        function (fileEntry) {  

          var oReq = new XMLHttpRequest();

          oReq.open("GET", downloadurl, true);
          oReq.responseType = "blob";
          oReq.onload = function (oEvent) {
            var blob = oReq.response;
            if (blob) {         
              fileEntry.createWriter(function (fileWriter) {                        
                fileWriter.onwriteend = function (e) {
                  console.log(fileEntry.toURL());// Works in Chrome and Firefox, but file URIs cannot be used for security reasons. So image is not displayed if this RUL is used in image src attribute
                  console.log(window.URL.createObjectURL(blob));// works in chrome and firefox. This is what I like to have in the end but using a file.

                  // Now I want to use the file and transform it in a objectURL, this is where I struggle
                  fileEntry.file(function(file){
                    //console.log(window.URL.createObjectURL(file));// works in chrome bot not in firefox. Firefox says :"TypeError: Argument 1 is not valid for any of the 1-argument overloads of URL.createObjectURL."

                    var readerBlob = new FileReader();                              
                    readerBlob.onload = function(event){                                    
                      console.log(event.target.result);

                      var blob = new Blob([event.target.result], {type: 'image/jpg'});
                      console.log(window.URL.createObjectURL(blob));// This URL will work in Chrome and Firefox          
                    };
                    readerBlob.readAsArrayBuffer(file);
                  });

                };
                fileWriter.onerror = function (e) {
                };
                fileWriter.write(blob);
              });

            } else console.error('we didnt get an XHR response!');
          };
          oReq.send(null);
        }, 
        function (error) {
          console.log('error creating file');
          console.log(error);
        }
      );
    }

    document.addEventListener("deviceready", function(){
      window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
      window.requestFileSystem(window.TEMPORARY, 10 * 1024*1024, function (fs) {
        filesystem = fs;            
        filesystem.root.getDirectory(
          '/ressources/',
          {create:true}, 
          function(dirEntry){
            console.log('download dir created');
            downloaddir = dirEntry;
            download();
          },
          function(error){
          }
        );
      });
    }, false);

以上是关于从本地文件系统文件条目创建createObjectURL的主要内容,如果未能解决你的问题,请参考以下文章

VBA createobject("wscript.shell").run 的问题?

BASH - 从 csv 文件的行创建数组,其中第一个条目是数组名称

防止导入时执行特定代码

将文件从 s3:// 复制到本地文件系统

如何从 Wscript.Echo strLine 创建文件

如何使用perl遍历一个目录下的所有文件