[英]IONIC 3 IOS can't read data from file
I am using this file picker to upload files to my server: 我正在使用此文件选择器将文件上传到我的服务器:
https://github.com/jcesarmobile/FilePicker-Phonegap-iOS-Plugin https://github.com/jcesarmobile/FilePicker-Phonegap-iOS-Plugin
My server takes base64 files, so I need to convert the file I uploaded. 我的服务器需要base64文件,所以我需要转换我上传的文件。 I am doing that using the file plugin mentioned in the ionic docs. 我正在使用离子文档中提到的文件插件。 So my code looks like this: 所以我的代码看起来像这样:
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)
}
)
}
but when I upload from Google Drive, or iCloud Drive or DropBox it returns 但是当我从Google云端硬盘或iCloud Drive或DropBox上传时,它会返回
{"code":5,"message":"ENCODING_ERR"} { “代码”: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=>{
})
I have fixed this issue using this code. 我已使用此代码修复了此问题。
I don't know much about the FilePicker you are using but why don't try with the Ionic native component to use the camera? 我不太了解您正在使用的FilePicker,但为什么不尝试使用Ionic本机组件来使用相机? Just give it a try to https://ionicframework.com/docs/native/camera/ it's really easy to use (there is an example in the link) I normally use it like this: 试试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);
});
}
The getPicture method returns a promise which returns either a base64 string or a file URI. getPicture方法返回一个promise,该promise返回base64字符串或文件URI。 In my case I return an URI but just by changing the options destinationType to this.camera.DestinationType.DATA_URL
you will get a base64 string and into the then statement: let base64Image = 'data:image/jpeg;base64,' + imageData;
在我的情况下,我返回一个URI,但只需将选项destinationType更改为this.camera.DestinationType.DATA_URL
您将获得一个base64字符串并进入then语句: let base64Image = 'data:image/jpeg;base64,' + imageData;
Hope it helps you! 希望它能帮到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.