【问题标题】:File image onloaded is not called after calling readAsDataURL on large images在大图像上调用 readAsDataURL 后不调用文件图像加载
【发布时间】:2016-11-02 15:15:54
【问题描述】:

我正在尝试读取图像文件并调整大小并将其上传到 Android 上的 Ionic2。我的代码适用于小尺寸图像,但对于超过 3MB 的图像,永远不会调用 onloaded 函数。

我非常感谢任何有关原因的帮助或见解。

makeFileIntoBlob(_imagePath) {


    return new Promise((resolve, reject) => {
        window.resolveLocalFileSystemURL( _imagePath, fileEntry => {

            fileEntry.file(file => {

                var reader = new FileReader();

                console.log('Reading the file!');

                reader.onloadend = (e: any) => {

                    var img = document.createElement("img");


                    img.onload = () => {
                        //---> Only get here for small images!
                        var resizedImg =  this.imgResizerSrvc.resize(img, 480, 480);
                        var imgBlob: any = this.dataURLtoBlob(resizedImg);
                        imgBlob.name = 'sample.jpg';
                        console.log('makeFileIntoBlob resolved!');
                        resolve(imgBlob);
                    }

                    //Will trigger img.onloadend
                    alert("about to trigger");
                    img.src = e.target.result;  //----> This call only triggeres reader.onloadend for small images
                    alert("Done trigger");

                };

                reader.readAsDataURL(file);

            });
        });
    });

}

作为替代方案,我也尝试使用以下方法读取文件:

img.src = (window.URL ? URL : window.webkitURL).createObjectURL( file );

img.src = (window.URL || window.webkitURL || window || {}).createObjectURL( file );

但这些都不会触发 img.onloaded()。

【问题讨论】:

  • .dataURLtoBlob 是做什么的? this.imgResizerSrvc.resize(img, 480, 480); 返回什么? .lengthdata URI 可能存在限制。 .this.onload 内是否引用 img 元素?
  • 它们用于调整大小和转换为 blob。我现在的问题是我什至没有调用这些函数,所以问题不应该在那里。此代码是 ionic2 中组件的成员函数,因此我预计 .this 正在引用该组件。但同样,我的问题是 img.onload 甚至没有被调用。
  • this inside img load 事件将是 <img> 元素。 data URI.length 可能存在限制。您可以使用Blob 而不是data URIfileFile 对象,是吗?

标签: javascript android html ionic-framework ionic2


【解决方案1】:

data URI.length 可能存在限制

长度限制

虽然 Firefox 本质上支持数据 URI 长度不限,浏览器不需要支持任何特定的 数据的最大长度。例如,Opera 11 浏览器限制数据 URI 长度约为 65000 个字符。

您可以使用URL.createObjectURL() 调用file,而不使用FileReader(),将.imgResizerSrvc.resize 创建的Blob 传递给File(),以设置创建File 对象的.name

makeFileIntoBlob(_imagePath) {

  return new Promise((resolve, reject) => {
    window.resolveLocalFileSystemURL(_imagePath, fileEntry => {

      fileEntry.file(file => {

        var component = /* reference to ionic2 component */ ;

        var img = document.createElement("img");

        img.onload = () => {

          var resizedImg = component.imgResizerSrvc.resize(this, 480, 480);
          var imgBlob = new File([resizedImg], "sample.jpg", {
            type: "image/jpeg"
          });
          resolve(imgBlob);

        };

        img.src = URL.createObjectURL(file);
      });
    });
  });
}

【讨论】:

  • 非常感谢您的回复。我试过你的代码,但我的代码在这一行崩溃了:img.src = URL.createObjectURL(file);任何尺寸的图像都会发生这种情况。我做了一些研究,其他建议的方法也不起作用:img.src = (window.URL ? URL : window.webkitURL).createObjectURL( file );img.src = (window.URL || window.webkitURL || window || {}).createObjectURL( file ); 有什么我想念的想法吗?
  • 什么是file
  • 据我了解,它只是一个由 window.resolveLocalFileSystemURL 返回的对象。我从这里获取了我的代码:github.com/aaronksaunders/firebaseStorage2/blob/master/app/…
  • console.log(file)console 登录什么?
  • 啊,它是一个对象,它有名称、localURLtypelastModifiedlastModifiedDatesizestartend 字段,但是文件这里的对象:https://developer.mozilla.org/en-US/docs/Web/API/File,略有不同。我如何转换它们?这些字段都是只读的。
猜你喜欢
  • 2015-12-29
  • 1970-01-01
  • 2014-08-15
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多