我正在尝试在离子应用程序中上传图像。上传在android中有效,但在iOS中却给了我错误。
FormData.append的参数2('blobValue')必须是 斑点
我使用以下内容捕获和分配图像
selectPhoto(): void {
this.camera.getPicture({
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
destinationType: this.camera.DestinationType.FILE_URI,
quality: 70,
allowEdit:true,
targetHeight: 300,
targetWidth: 300,
encodingType: this.camera.EncodingType.PNG,
}).then(imageData => {
this.image = normalizeURL(imageData);
this.uploadPhoto(imageData);
}, error => {
this.func.presentToast(JSON.stringify(error), 3000, 'bottom')
});
}
uploadPhoto(imageFileUri: any): void {
this.file.resolveLocalFilesystemUrl(imageFileUri)
.then(entry => (<FileEntry>entry).file(file => this.readFile(file)))
.catch(err => console.log('Error',JSON.stringify(err)));
}
private readFile(file: any) {
const reader = new FileReader();
reader.onloadend = () => {
const imgBlob = new Blob([reader.result], { type: file.type });
this.imageDataUri = imgBlob;
this.imageName = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
// alert(this.imageName)
};
reader.readAsArrayBuffer(file);
}
并将blob分配为表单数据
formData.append('image', this.imageDataUri, this.imageName)
但是我仅在IOS中收到上述错误。
离子信息:
Ionic:
ionic (Ionic CLI) : 4.8.0 (/usr/local/lib/node_modules/ionic)
Ionic Framework : ionic-angular 3.9.2
@ionic/app-scripts : 3.2.1
Cordova:
cordova (Cordova CLI) : 8.1.2 (cordova-lib@8.1.1)
Cordova Platforms : android 6.4.0, browser 5.0.4, ios 4.5.5
Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.3, (and 12 other plugins)
System:
ios-deploy : 1.9.4
NodeJS : v10.15.0 (/usr/local/bin/node)
npm : 6.4.1
OS : macOS Mojave
Xcode : Xcode 10.1 Build version 10B61