Last active
May 6, 2019 01:52
-
-
Save sean-hill/4abcec4577a44195db51 to your computer and use it in GitHub Desktop.
Upload service for Ionic and ngCordova
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ionic and ngCordova upload example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Upload Ctrl | |
angular.module('starter.controllers') | |
.controller('UploadCtrl', function($scope, Upload){ | |
$scope.uploadFile = function() { | |
Upload.fileTo(<your server api url>).then( | |
function(res) { | |
// Success | |
}, function(err) { | |
// Error | |
}) | |
; | |
}; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Upload Service | |
angular.module('starter.services') | |
.factory('Upload', function($q, $cordovaCamera, $cordovaFile, Constants) { | |
return { | |
fileTo: function(serverURL) { | |
var deferred = $q.defer(); | |
if (ionic.Platform.isWebView()) { | |
var options = { | |
quality: 100 | |
, destinationType: Camera.DestinationType.FILE_URI | |
, sourceType: Camera.PictureSourceType.PHOTOLIBRARY | |
, encodingType: Camera.EncodingType.JPEG | |
} | |
$cordovaCamera.getPicture(options).then( | |
function(fileURL) { | |
var uploadOptions = new FileUploadOptions(); | |
uploadOptions.fileKey = "file"; | |
uploadOptions.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1); | |
uploadOptions.mimeType = "image/jpeg"; | |
uploadOptions.chunkedMode = false; | |
$cordovaFile.uploadFile(serverURL, fileURL, uploadOptions).then( | |
function(result) { | |
deferred.resolve(result); | |
}, function(err) { | |
deferred.reject(err); | |
}) | |
; | |
}, function(err){ | |
deferred.reject(err); | |
}) | |
; | |
} | |
else { | |
deferred.reject('Uploading not supported in browser'); | |
} | |
return deferred.promise; | |
} | |
} | |
}) |
$cordovaFile should be $cordovaFileTransfer with and upload
method
What's Constants in a factory Upload declaration?
is there for lan,, without connection internet?
$cordovaFile ---> $cordovaFileTransfer
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Implemented this same some time back, very good but did this on .net API