Trying to upload of FileTransfer image and user data in Ionic 2?

366 Views Asked by At

I am trying to send to my endpoint, user image plus all user data. I take the user image from the device gallery and it has this path:

let imageLocation = file:///data/user/0/.../cache/tmp_IMG-20170907-WA0026534743393.jpg;

Then I have the user object:

this.dataUser = {
    name: "jhon",
    email: "[email protected]",
    id: 123
};

and now I create the TransferObject object and implement the upload function:

const fileTransfer: TransferObject = this.transfer.create();
let options: FileUploadOptions = {
    fileKey: 'avatar',
    httpMethod: 'PUT',
    headers: { 'Authorization': 'JWT ' + token },
    params: this.dataUser
}

return new Promise(resolve => {
  fileTransfer.upload(imageLocation, URL, options, true)
              .then((data) => {
                console.log('upload success -> ', data);
                resolve(data);                 
              }, error => {
                console.log(error);
              });
});

When the function is executed, the upload seems to be successful as it returns the object with responseCode = 200, but the response key holds an empty string, why?

This is my upload output:

{bytesSent: 488723, responseCode: 200, response: "{"avatar": null, "name": "", "email": "", "id": null}"}
0

There are 0 best solutions below