Search code examples
ioscordovaionic-frameworkcordova-pluginsngcordova

Cordova file upload error on IOS


I can't get my Image Upload to work on iOS. It works fine on Android but iOS is causing problems. Here is my code:

   var server = URLS.INDEX + 'items';
            var options = {
                fileKey: 'itemPic',
                params: newItemData,
                chunkedMode : false,
                headers: {
                    'x-access-token': authService.getApiToken(),
                    Connection: "close"
                }
            };
            $cordovaFileTransfer.upload(server, filePath, options)
                .then(function (result) {
                    postNewItemPromise.resolve();
                    $state.go('slider.me', {}, {
                        reload: true
                    }).then(function () {
                        $rootScope.$broadcast('newItemAdded');
                    });
                }, function (err) {
                    postNewItemPromise.reject();
                    if (err.status === 401) {
                        authService.reAuth();
                    }
                }, function (progress) {
                });

I obtain the following error:

body: "{\"message\":\"Unexpected token u\",\"error\":{}}"
code: 3
exception: null
http_status: 400
source: "file:///var/mobile/Containers/Data/Application/7B768C78-48F1-4A8B-BDC8-35F1C93AEC76/tmp/cdv_photo_007.jpg"
target: "xxxx"

I found many posts about similar problems but they all fixed this with Connection : "close" and chunkedMode : false. This is not working for me though.

I think the problem is that the file transfer plugin is not able to parse an array and an object inside my newItemData object properly. I don't know how to fix that though.


Solution

  • I fixed the issue myself. iOS isn't able to pares objects inside params object, so i had to convert them to json strings beforehand.