我正在将多个图像(jpeg)上传到我的api服务器。该代码过去一直可以正常工作并且运行了几个月,但90%的时间开始失败。
promises = [];
constructor(public navCtrl: NavController,...,public file: File) {
uploadImage(imgArr) {
//Hardcored to test for single image
console.log("Came here");
this.file.readAsDataURL(this.image[0].substr(0,
this.image[0].lastIndexOf('/') + 1),
this.image[0].substr(this.image[0].lastIndexOf('/') + 1)).then(result => {
console.log(result);
}).catch((err)=> {
console.log(err);
});;
console.log("Came here too but never run readAsDataURL");
//Ran readAsDataURL at times
}
uploadImage2(imgArr) {
//Original Code
console.log("Came here");
for (let i = 0; i<this.image.length;i++) {
console.log("Looped Here");
let promise =
this.file.readAsDataURL(this.image[i].substr(0, this.image[i].lastIndexOf('/') + 1), this.image[i].substr(this.image[i].lastIndexOf('/') + 1)).then(result =>
{
console.log(result);
}).catch((err)=> {
console.log("ERROR",err);
});
this.promises.push(promise);
}
console.log("Came here too but never run readAsDataURL");
//Log this.promises shows length > 0 but all null
//Ran readAsDataURL at times
}
uploadImage3(imgArr) {
//Modify to use array map
this.promises = this.image.map( img => {
console.log(img);
this.file.readAsDataURL(img.substr(0, img.lastIndexOf('/') + 1), img.substr(img.lastIndexOf('/') + 1)).then((result) => {
console.log(result);
}).catch ((err)=> {
console.log('ERROR',err);
});
});
//Same results at uploadImage2
}
}
我只需要它来读取文件。
*文件和文件夹存在
*文件路径(例如file:///storage/emulated/0/Android/data/***/cache/1560382588088.jpg
离子:
离子(离子CLI):4.7.1 离子框架:离子角3.9.2 @ ionic / app-scripts:3.2.4科尔多瓦:
cordova(Cordova CLI):8.0.0 Cordova平台:Android 7.0.0 Cordova插件:cordova-plugin-ionic-keyboard 2.1.3,cordova-plugin-ionic-webview 2.3.1(和其他10个插件)
@ ionic-native /文件4.20.0
插件:cordova-plugin-file 来源:npm://cordova-plugin-file@^6.0.1 安装的版本:6.0.1 远程版本:6.0.1