diff --git a/README.md b/README.md index 4bde5f2..6e895b3 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,38 @@ angular.module('myApp') }); ``` +### FileTransfer +The `FileTransfer` service works similarly to the PhoneGap/Cordova version, except that the `upload` and `download` methods return promises. The `FileTransferError` service exports the PhoneGap `FileTransferError` error codes. + +```javascript +angular.module('myApp') + .controller('MyController', function($scope, FileTransfer, FileTransferError) { + var ft = new FileTransfer(); + + // ... + + ft.upload(fileURL, destinationURL, options, true).then(function(res) { + + $scope.message = 'Uploaded ' + res.bytesSent + ' bytes!'; + + }, function(error) { + + if (error.code === FileTransferError.ABORT_ERR) { + $scope.message = 'Uploading aborted!'; + } else { + $scope.message = 'Error while uploading!'; + } + + }, function(progress) { + + $scope.loaded = progress.loaded; + + }); + + }); + +``` + ### Globalization TODO diff --git a/src/plugins/FileTransfer.js b/src/plugins/FileTransfer.js new file mode 100644 index 0000000..1fb8bda --- /dev/null +++ b/src/plugins/FileTransfer.js @@ -0,0 +1,76 @@ +'use strict'; + +angular.module('PhoneGap') + .factory('FileTransfer', function (PhoneGap, $q, $window) { + + var ngFileTransfer = function () { + var self = this; + + this.$deferred = $q.defer(); + this.$dirty = false; + + PhoneGap.ready().then(function () { + self.$ft = new $window.FileTransfer(); + self.$ft.onprogress = angular.bind(self.$deferred, self.$deferred.notify); + }); + + }; + + angular.forEach([ 'upload', 'download' ], function (action) { + ngFileTransfer.prototype[action] = function (src, dst, options, trustAllHosts) { + var self = this, + onSuccess = angular.bind(this.$deferred, this.$deferred.resolve), + onError = angular.bind(this.$deferred, this.$deferred.reject); + + // The internal state of the PhoneGap FileTransfer object is not + // documented. Better safe than sorry + if (this.$dirty) { + throw new Error('FileTransfer object already used!'); + } + + this.$dirty = true; + + if (arguments.length < 4) { + trustAllHosts = false; + } + + options = options || {}; + + return PhoneGap.ready().then(function () { + self.$ft[action](src, dst, onSuccess, onError, options, trustAllHosts); + return self.$deferred.promise; + }); + }; + }); + + ngFileTransfer.prototype.abort = function () { + var self = this; + + PhoneGap.ready().then(function () { + self.$ft.abort(); + }); + }; + + return ngFileTransfer; + }); + +angular.module('PhoneGap').factory('FileTransferError', function (PhoneGap, $window, $log) { + + var FileTransferError = {}; + + PhoneGap.ready().then(function() { + if (!$window.FileTransferError) { + $log.error(new Error('FileTransferError not loaded!')); + return; + } + + angular.extend(FileTransferError, { + FILE_NOT_FOUND_ERR: $window.FileTransferError.FILE_NOT_FOUND_ERR, + INVALID_URL_ERR: $window.FileTransferError.INVALID_URL_ERR, + CONNECTION_ERR: $window.FileTransferError.CONNECTION_ERR, + ABORT_ERR: $window.FileTransferError.ABORT_ERR, + }); + }); + + return FileTransferError; +}); \ No newline at end of file