Reputation: 372
I've ran into some problems while using cordova plugin File Transfer. Thats my code:
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0,
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile(
"dummy.html", {create: true, exclusive: false},
function gotFileEntry(fileEntry) {
var sPath = fileEntry.fullPath.replace("dummy.html", "");
var fileTransfer = new FileTransfer();
fileEntry.remove();
fileTransfer.download( 'http://cordova.apache.org/images/cordova_bot.png', sPath + photo.original_name,
function (theFile) {
alert('success: ' + JSON.stringify(theFile));
console.log("download complete: " + theFile.toURI());
// showLink(theFile.toURI());
},
function (error) {
alert('error: ' + JSON.stringify(error));
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code: " + error.code);
},
true
);
})
},
function (error) {
alert('error request: ' + JSON.stringify(error));
}
);
The fileTransfer.download's error callback is being returned with error code 3, http 401. I already updated the File and FileTransfer plugins, my cordova version is 4.3.0. Also checked my config.xml for
<access origin="*" />
but it's there. I tried adding the header Connection: close, but no result. Tried setting the download's 4th param to its default (false) too - no luck.
Testing on Android tablet.
Anyone anything? Thanks!
Upvotes: 2
Views: 12425
Reputation: 21
My problem was related to ad http (not https) remote file url.
I solved with this steps:
Upvotes: 0
Reputation: 1
This worked for me
let pathToDownload = `${cordova.file.externalDataDirectory}`;
let fileTransfer = new FileTransfer();
let url = encodeURI(`https://cordova.apache.org/images/cordova_bot.png`);
fileTransfer.download(url, pathToDownload + "image.png",
(fileEntry) => {
console.log('Downloaded!');
},
(error) => {
console.log('Download error');
},
);
Upvotes: 0
Reputation: 1
Just change the download url to http://www.{{Domain name}}/abc.pdf
use full url with www
Upvotes: 0
Reputation: 8726
If your remote file needs VPN to access, make sure your device is connected to VPN.
In my case I was not connected to VPN on device.
Upvotes: 0
Reputation: 372
Just found a "solution" to my problem. What I did was to downgrade the file-transfer plugin version from 0.5.0 to 0.4.8.
If someone ever face similar problem, do as below:
That's it. Seems to be working well, at least the success callback is returned, didn't really test more of it.
Upvotes: 4
Reputation: 6242
I personally wouldn't create a file then remove it just to get a directory URL. You should be able to obtain just that by doing fileSystem.root.toURL()
- fileSystem.root is a DirectoryEntry and so contains methods you'd expect to see on a DirectoryEntry.
Just a bit quicker.
Update
If you feel inclined to use the file delete method, you should be using toURL() on the FileEntry, not fullPath. I think toURL() returns a URL that can be used throughout HTML app.
But as I say, fileSystem.root.toURL() is preferable. Example code as follows:
Thus, your code becomes:
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0,
function (fileSystem) {
var url = 'http://cordova.apache.org/images/cordova_bot.png',
dir = fileSystem.root.toURL() + photo.original_name,
ft = new FileTransfer();
ft.download(url, dir,
function (fileEntry) {
alert('Downloaded!');
},
function (error) {
alert('Download error');
console.log(dir);
}
);
},
function (error) {
alert('Error getting file system');
}
);
Try that and see what happens. Might sound stupid, but I presume photo.original_name
is defined? And there's a '/' between the directory and the filename?
Upvotes: 1