Upload image from Camera/Gallery to server


I am following this tutorial for uploading image on server - https://devdactic.com/ionic-2-images/

After implementing, I am getting an error while copying file to local directory, this is the function -

copyFileToLocalDir(namePath, currentName, newFileName) {
this.file.copyFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(success => {
this.lastImage = newFileName;
}, error => {
this.presentToast(‘Error while storing file.’);

I am receiving toast message as “Error while storing file”. Please help me out to resolve this issue. Thanks.

But what’s the actual error?

{code:5, message:encoding_err}

Let me perhaps go a step back, is there a reason you’re copying the file first, rather than just uploading it? Let me grab you some code from my last project, I did image uploads and I don’t think I did that.

yes please, will try your code

So, my app submitted a whole form with a bunch of other data with the images, so I’m not sure how much my code will help you directly, but I will say we definitely do not copy the files over. I don’t know why you’d need to.

You will already have the path to the image, can’t you just use that and directly upload it? That’s what we did.

I have the same requirement. I also want to submit a bunch of data with the images. When I click an image from the camera, this works fine. The issue occurs when I try to upload through the gallery.

Okay, so what if you don’t copy an of the files. That would be my first suggestion, just skip that.

Second, can you log out the newFileName before you do your copying so we can see what those new file names are?

I am able to upload a single image, can you tell me how to upload a form with multiple images ?

hi, can i take a look at your coding for upload picture to server? because i’m facing some bug. my image didn’t upload to server but there are no error message.

Hello @sajalsuraj,

This code is outdated now (will not work), if you use Ionic 3 latest version.

You’ll need to tweak it a bit, I can help if you want.



Hello @FrancoisIonic
I’m using ionic 3
i get this error with android but ios works well
can you help me ?
thanks in advance.

Hello, yes of course I only did minor gits to that code and it works well with TS 1.0 (tested on Android + iOS).
What is your question please ?

i think your error is related to a missing package of Angular, can you check please ? (i had to install it on my end and include in the package for some time utilities and classes).

thanks for your reply,
can you think any of packages i missed?
Do you prefer reinstall packages again ?
PS : works well with ios .