You can test your code on your phone! Open Safari on your Mac and open your app afterwards. Then in Safari go to Developers -> Your phone. A debug console like Chrome´s console opens. There you can debug your code! Your phone needs to be plugged to your Mac ofc Hope this helps.
I would use destinationType : Camera.DestinationType.FILE_URL.
And using the FileTransfer plugin of cordova or http://ngcordova.com/docs/#File to upload the file.
(alerts are not pretty I know, but they should work in Your case)
I would also suggested reading about deferred API in Angular link to Angular docs . This is what I needed to use for file upload in a web app I was building. So Your service would look something like this:
yourServices.factory('fileUpload', ['$q', '$http',
function ($q, $http) {
var uploadFileToUrl = function(uploadUrl) {
var deferred = $q.defer();
$http.post(uploadUrl).success(deferred.resolve).error(deferred.reject);
return deferred.promise;
}
};
return {
uploadFileToUrl : uploadFileToUrl
};
}]
);
I would personally prefer to append the file in the controller, but that’s just my preference I think
If You go on to making something bigger (with file preview etc.) I recommend this: Angular File Upload
Check the getPicture() function. how do i pass the key ‘avatar’ to the post and how do i pass the format of the post? and what should i put instead of the the options?
ftOptions = new FileUploadOptions();
ftOptions.fileKey = 'file'; // filekey
ftOptions.fileName = params.substr(params.lastIndexOf('/') + 1); // set file name (last part of the image path)
ftOptions.mimeType = 'image/jpeg';
ftOptions.httpMethod = 'PUT'; // post, put...
ftOptions.headers = { }; // to set authorization headers and so on
Hey i set my destinationType for the camera plugin to:
$window.navigator.camera.DestinationType.FILE_URI
Then you will get the correct file-path in your filesystem.
But if you are using ngCordova
Camera.DestinationType.FILE_URI
should do it
Because the file-transfer plugin only handles file-paths.
Another idea:
you do not set your http-method so i think it will automatically send a post request? do not know how the plugin works.
I set for the original plugin the following options:
ftOptions = new FileUploadOptions();
ftOptions.fileKey = 'file';
ftOptions.fileName = params.substr(params.lastIndexOf('/') + 1);
ftOptions.mimeType = 'image/jpeg'; (mime type of the file - if you know it)
ftOptions.httpMethod = 'PUT'; // the method your api provides for the upload (should be post or put)
i do not know if there is a matching fot those settings in ngCordova… could not find anything in their docs or maybe you can pass FileUploadOptions object directly
Got it working. Thanks a lot @bengtler. The ngCordova docs do not include the ftOptions section. Lesson for me to always read the cordova docs first. Really appreciate the help.
Updated, working example for anyone interested. @bengtler are you using the same options and structure for Android + iOS?