File-transfer is not working in iOS (ionic 3)

#1

Hi. I have a very strange problem with @ionic-native/file-transfer in ionic 3. in Android the upload works very well, but in iOS it doesn’t work, when I send the request nothing happens, neither a 200 or any other response.

This is my code

public doSelectPhotoImage (sourceType:number, uploadURL:string) {
      
      const options: CameraOptions = {
        quality         : 80,
        destinationType : this.camera.DestinationType.FILE_URI,
        sourceType      : sourceType, // this.camera.PictureSourceType.CAMERA
        encodingType    : this.camera.EncodingType.JPEG,
        mediaType       : this.camera.MediaType.PICTURE
      }
      
      this.camera.getPicture(options).then((imagePath) => {
        
        // Special handling for Android library
        if (this.isPlatformAndroid()) { 
          if (sourceType !== this.camera.PictureSourceType.CAMERA) {
            this.filePath.resolveNativePath(imagePath)
              .then(filePath => {
                this.sendFileAndData(filePath, uploadURL, {});
            });
          } else {
            this.sendFileAndData(imagePath, uploadURL, {});
          }
          
        } if (this.isPlatformIOS()) {
          this.sendFileAndData(normalizeURL(imagePath), uploadURL, {});
          
        } else {
          this.sendFileAndData(imagePath, uploadURL, {});
        }
        
      }, (err) => {
        console.log(err);
      });
      
  }



  public sendFileAndData (fileURL:string, uploadURL:string, params) {

    console.log("fileURL=" + fileURL);
    
    let options: FileUploadOptions = {
      fileKey: "fileParamName",
      chunkedMode: false,
      params: params
    };
    
    this.cordovaFileTransfer.upload(fileURL, uploadURL, options).then(
      (result) => {
        console.log("Result: " + result);
      }, (err) => {
        console.log("ERROR: " + err);
      }
    ).catch((err) => {
      console.log("ERR: " + err);
    });
    
  }
  

Nothing is printed in the console.

I also have read and follow the instructions in this article without success:

I have uninstalled and re-installed the plugin and the dependencies of file-transfer, also re-installed all the project dependencies, but the result is the same.

I have tested in the emulator and in a real device, iPhone 6 with iOS 11.

ionic-info:
ionic-info

I have spent 2 weeks without success, any help will be very appreciated.

Thanks in advance!

#2

May be you could try with base64 data.
I’m uploading images with base64 data without file transfer and it’s working fine.
Below is the code, I wrote to achieve this.
I’m promting a user to choose photo from library or click a new one using camera.

uploadImage() {
    let actionSheet = this.actionSheetCtrl.create({
        title: "Select Image Source",
        buttons: [{
                text: "Load from Library",
                icon: "images",
                handler: () => {
                    this.selectAction(0);
                }
            },
            {
                text: "Use Camera",
                icon: "camera",
                handler: () => {
                    this.selectAction(1);
                }
            }
        ]
    });
    actionSheet.present();
}

options: CameraOptions = {
    quality: 100,
    destinationType: this.camera.DestinationType.DATA_URL,
    encodingType: this.camera.EncodingType.JPEG,
    sourceType: this.camera.PictureSourceType.SAVEDPHOTOALBUM,
    saveToPhotoAlbum: true,
    allowEdit: true,
    targetWidth: 200,
    targetHeight: 200
};

selectAction(actionType) {
    if (actionType === 0) {
        this.options.sourceType = this.camera.PictureSourceType.SAVEDPHOTOALBUM;
    } else if (actionType === 1) {
        this.options.sourceType = this.camera.PictureSourceType.CAMERA;
    }
    this.camera.getPicture(this.options).then(imageData => {
        let apiUrl = API_HOST + "profile-sd.php";
        let param = { base64Image: imageData };
        this.dataServiceProvider.postService(apiUrl, param).subscribe(res => {
            if (res.status && !res.error) {
                // upload successful
            } else {
                toastOptions.message = res.message;
                this.showToast();
            }
        }, error => {
            console.error(error);
            toastOptions.message = this.labels.SOMETHING_WENT_WRONG;
            this.showToast();
        });
    }, err => {});
}

Hope this will help you.

#3

As @Heenavora said try using base64 for image upload.

#4

Thanks for your suggestion. I was doing lots of tests, but nothings works in iOS with file-transfer.

I have implemented the upload with a base64 post parameter, it works fine although it’s heavier and slower.

Now the problem is the download, file-transfer does not work, how can I implement file downloading in iOnic 3 for iOS?