How to upload a picture with Ionic 2

Hi, I’m building an app with Ionic 2 and Django Rest Framework. I need to take a photo from gallery or camera and upload this picture to my server.

I have this code that opens the camera and takes a picture.

options = {}
Camera.getPicture(options).then((imageData) => {
    // imageData is either a base64 encoded string or a file URI
    // If it's base64:
    let base64Image = "data:image/jpeg;base64," + imageData;
}, (err) => {
}); 

But I don’t know where it saves the pictures or how can I send it to the server. I don’t find anything on Internet.

Thanks

1 Like

You’ll want to pass in options to getPicture(), specifically:

options = {destinationType: 1}; /*Camera.DestinationType.FILE_URI*/

then install and use the cordova-plugin-file-transfer plugin to upload the file path to a destination url

In addition to @jasonwaters answer. I have a working example of the FileTransfer plugin here. The only part that is not working is updating the progress to view. Still working on that part. So it will upload your files no problem. :slight_smile:

Why not send the base64 image to server?

2 Likes

Finally I sent the image in base64 to the server. It’s working great.

It’s faster this way?

Thanks

Hey,
Would you mind sharing the code?

I need to do something similar! :slight_smile:

Thanks!

hey can you share the code? my api layer is on DRF as well, loving it so far :slight_smile: