I am trying to implement attachment function for my app. I got the problem that onloadend has never been reached (most of time). I get the FileEntry and File correctly so. However, it works mysteriously rarely but after I build new one or just restart an app, it won’t work anymore. I did some research and these is some people maybe get the same problem with me but I can not find out proper solution for my case.
Any advice?
My code:
attachFile(attachFab?: FabContainer) {
if (attachFab) {
attachFab.close();
}
let that = this;
this.platform.ready()
.then(() => {
let loader = that.loadingCtrl.create({
content: 'Saving...',
});
let attach: IrAttachmentInstance = new IrAttachmentInstance(this.irAttachmentModelService, {});
if (this.platform.is('android')) {
FileChooser.open()
.then((uri) => {
window.resolveLocalFileSystemURL(uri, (fileEntry) => {
console.log('get file entry: ', fileEntry);
// file info here
fileEntry.file((file) => {
console.log('get file info: ', file);
attach.name = fileEntry.name;
attach.res_name = fileEntry.name;
attach.file_type = file.type;
attach.mimetype = file.type;
attach.res_model = 'hr.expense.expense';
attach.res_id = that.expense_id;
let fileReader: FileReader = new FileReader();
fileReader.onloadend = function(e) {
console.log('get file data event', e);
attach.datas = btoa(fileReader.result);
// save to db
Promise.resolve(loader.present())
.then(() => attach.save().toPromise())
.finally(() => loader.dismiss())
.then((data) => {
return Promise.try(() => {
that.events.publish(
constants.EVENT_KEY_MODEL_CREATED(
that.irAttachmentModelService.name),
attach.id,
);
})
.then(() => that.toastCtrl.create({
message: 'Success.',
duration: 3000,
}).present());
}, (err) => that.toastCtrl.create({
message: err.message || 'Failed. Please try again.',
duration: 3000,
}).present());
};
fileReader.readAsBinaryString(file);
});
});
});
}
});
}
My system information:
Cordova CLI: 6.5.0
Ionic Framework Version: 2.0.0-rc.1
Ionic CLI Version: 2.2.1
Ionic App Lib Version: 2.2.0
Ionic App Scripts Version: 0.0.38
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Linux 4.9
Node Version: v7.5.0
Xcode version: Not installed