Upload image from Camera/Gallery to server

#1

Hi,

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;
this.presentToast(this.lastImage);
}, 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.

#2

But what’s the actual error?

#3

{code:5, message:encoding_err}

#4

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.

#5

yes please, will try your code

#6

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.

#7

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.

#8

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?

#9

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

#10

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.

#12

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.

Regards,

FrancoisIonic

#13

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

#14

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 ?

#15

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).

#16

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