Skip to content
This repository was archived by the owner on Jul 16, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
76 changes: 76 additions & 0 deletions src/plugins/FileTransfer.js
Original file line number Diff line number Diff line change
@@ -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;
});