I am attempting to retrieve a file from an FTP server using Cordova. To accomplish this, I have installed the ngCordova and Filetransfer plugins. Below is the snippet of code I am working with:
angular.module('TestApp', ['ionic', 'TestApp.controllers', 'charts.ng.justgage', 'pascalprecht.translate', 'ngCookies', 'ngCordova'])
var app = angular.module('TestApp.controllers', [])
app.controller('FileDownloadCtrl', function($scope, $timeout, $cordovaFileTransfer) {
$scope.downloadFile = function() {
var url = "ftp://URL/somefile.xml";
var filename = url.split("/").pop();
alert(filename);
var targetPath = cordova.file.externalRootDirectory + filename;
var trustHosts = true;
var options = new Object();
var headers = { 'Authorization': 'Basic Login_cred' };
options.headers = headers;
alert(cordova.file.externalRootDirectory);
alert(options.headers);
$cordovaFileTransfer.download(url, targetPath, options, trustHosts)
.then(function(result) {
// Success!
alert(JSON.stringify(result));
}, function(error) {
// Error
alert(JSON.stringify(error));
}, function (progress) {
$timeout(function () {
$scope.downloadProgress = (progress.loaded / progress.total) * 100;
})
});
}
});
The Angular.module can be found in a separate JS file within the actual code.
After running this code, the following alert messages are displayed:
- somefile.xml
- file://storage/emulated/0/
- [object Object]
- {"code":2,"source":","target":file:///storage/emulated/0/somefile.xml","http_status": null,"body":null,"exception":null}
I have attempted to download a file from a website hosted on the FTP (), which was successful. However, I suspect there may be an issue with the header setup in my code. Moreover, I tried including the Login_creds in the target URL like so: , but this approach did not work, possibly due to browser-specific restrictions.