angularjs - Take in-app photo with Cordova Camera and upload it to Parse.com -
i'm building ionic/cordova app uses parse.com baas. uses ngcordova camera plugin control device camera. use-case click button, take picture , have upload parse. i've been researching problem week , still can't figure out why can't work.
the controller:
.controller('cameractrl', function($scope, camera) { var cameraoptions = { quality: 75, destinationtype: 0, encodingtype: 0, targetwidth: 300, targetheight: 300, mediatype: 0, correctorientation: true, savetophotoalbum: true }; }; $scope.takepicture = function() { cameraoptions.sourcetype = 1; navigator.camera.getpicture(onsuccess, onfail, cameraoptions); } $scope.selectpicture = function() { cameraoptions.sourcetype = 0; navigator.camera.getpicture(onsuccess, onfail, cameraoptions); } function onsuccess(picture) { file.upload(picture) .success(function(data) { // upload finish }); $scope.$apply(function() { $scope.preview = 'data:image/jpeg;base64,' + picture; }); } function onfail(resp) { alert('error: ' + resp); } });
the service:
angular.factory('file', function ($http) { return { upload: function (photo) { var json = { 'base64': photo, '_contenttype': 'image/jpeg' } var config = { method: 'post', url: 'https://api.parse.com/1/files/pict.jpg', data: json, headers: { 'x-parse-application-id': 'pcm0kdvethvrcdfus9litrmskehqjbqwfaydl2lr', 'x-parse-rest-api-key': 'fhasgktl0blpjulljvpb2nfwlccxzvbirktdngxn' } }; return $http(config); } } });
the html:
<button class="button" ng-click="takepicture()">
any ideas why doesn't work? there better or more simple way accomplish this? examples of working somewhere? i've tried dozen different solutions on week , haven't found works use-case. thanks!
it helpful provided error messages, here how have solved issue
var imagefile = new parse.file("mypic.jpg", {base64: _params.photo}); console.log(imagefile); // save parse file return imagefile.save().then(function () { // create object hold caption , file reference var imageobject = new imageobject(); // set object properties imageobject.set("caption", _params.caption); imageobject.set("picture", imagefile); // save object parse backend return imageobject.save(); }, function (error) { console.log("error"); console.log(error); });
there complete project here showing parse.com integration file object.
https://github.com/aaronksaunders/dcww/blob/master/www/js/services.js
Comments
Post a Comment