【发布时间】: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);返回什么?.length或data URI可能存在限制。.this在.onload内是否引用img元素? -
它们用于调整大小和转换为 blob。我现在的问题是我什至没有调用这些函数,所以问题不应该在那里。此代码是 ionic2 中组件的成员函数,因此我预计
.this正在引用该组件。但同样,我的问题是img.onload甚至没有被调用。 -
thisinsideimgload事件将是<img>元素。data URI的.length可能存在限制。您可以使用Blob而不是data URI。file是File对象,是吗?
标签: javascript android html ionic-framework ionic2