IONIC 3 IOS无法从文件中读取数据

noo*_*oor 9 ios cordova ionic3 angular

我正在使用此文件选择器将文件上传到我的服务器:

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)
        }
    )
}
Run Code Online (Sandbox Code Playgroud)

但是当我从Google云端硬盘或iCloud Drive或DropBox上传时,它会返回

{ "代码":5 "消息": "ENCODING_ERR"}

小智 0

我对您正在使用的 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);
    });
  }
Run Code Online (Sandbox Code Playgroud)

getPicture 方法返回一个 Promise,该 Promise 返回 Base64 字符串或文件 URI。在我的例子中,我返回一个 URI,但只需将选项 destinationType 更改为this.camera.DestinationType.DATA_URL即可获得一个 base64 字符串并放入 then 语句中:let base64Image = 'data:image/jpeg;base64,' + imageData;

希望对您有帮助!