Many times in our mobile application we are required to upload or transfer files to server, although simply transferring files or uploading file can be easy task but in cordova one must follow the procedure provided by cordova. Multiple parameters are required to keep in mind like, what kind of form data or content you are going to post ie. multipart-form-data or application/base64 etc.
But keeping things simple we are going to post multipart-form-data and image application/base64. Don’t worry just forget these terms if you are newbie here, just follow the code steps and you will get the desired result.
Notice here that we are assuming you are ready with your api call to upload image, here we are focusing on uploading the image using the api call.
What you need:
You have to use : $cordovaFileTransfer plugin.
1- Taking photo via accessing camera in cordova, read our previous blog.
2- Or selecting picture from gallery
Generally we separate the service/factory and controller in angularjs but for simplifying the example we are combing them in controller.
For the better explanation i am commenting the code properly.
upload(server, targetPath, options, trustAllHosts)
||URL of the server to receive the file|
||Filesystem url representing the file on the device|
||If set to
For better plugin explanations and usage please read out official documentation: here