[英]IONIC 3 IOS can't read data from file
我正在使用此文件選擇器將文件上傳到我的服務器:
https://github.com/jcesarmobile/FilePicker-Phonegap-iOS-Plugin
我的服務器需要base64文件,所以我需要轉換我上傳的文件。 我正在使用離子文檔中提到的文件插件。 所以我的代碼看起來像這樣:
uploadIOS(){
var self=this
let utis = ["public.data"]
FilePicker.pickFile(
function (uri) {
let correctPath = uri.substr(0, uri.lastIndexOf('/') + 1);
let currentName = uri.substring(uri.lastIndexOf('/') + 1);
self.file.readAsDataURL(correctPath, currentName).then(result=>{
console.log ('reading data ' + JSON.stringify(result))
}).catch((err)=>{
console.log ('err4' + JSON.stringify(err))
})
},
function (error) {
console.log(JSON.stringify(error));
},
function (utis) {
console.log('UTIS', this.utis)
}
)
}
但是當我從Google雲端硬盤或iCloud Drive或DropBox上傳時,它會返回
{ “代碼”:5 “消息”: “ENCODING_ERR”}
let self = this;
self.filePicker.pickFile().then(uri => {
let correctPath = uri.substr(0, uri.lastIndexOf('/') + 1);
let currentName = uri.substring(uri.lastIndexOf('/') + 1);
self.file.readAsDataURL("file:///" + correctPath, currentName).then(result=>{
})
我已使用此代碼修復了此問題。
我不太了解您正在使用的FilePicker,但為什么不嘗試使用Ionic本機組件來使用相機? 試試https://ionicframework.com/docs/native/camera/它真的很容易使用(鏈接中有一個例子)我通常使用它像這樣:
public selectFromGallery() {
var options: CameraOptions = {
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
destinationType: this.camera.DestinationType.FILE_URI,
targetWidth: 640,
targetHeight: 480,
correctOrientation: true,
quality: 100
};
this.camera.getPicture(options).then((imageData) => {
return this.makeFileIntoBlob(imageData);
}).then((imageBlob) => {
this.cameraData = imageBlob;
this.imageUpload(this.createFileName());
}, (err) => {
this.db.logMessage(new LogEntity("Error while selecting image", this.user.uid, JSON.stringify(err)), LogType.Error);
this.presentToast(this.translations.code_image);
});
}
getPicture方法返回一個promise,該promise返回base64字符串或文件URI。 在我的情況下,我返回一個URI,但只需將選項destinationType更改為this.camera.DestinationType.DATA_URL
您將獲得一個base64字符串並進入then語句: let base64Image = 'data:image/jpeg;base64,' + imageData;
希望它能幫到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.