I want to use cordova file and file transfer to download file, but I don't know how to do it?
Please point me to any hint. Thanks!
I want to use cordova file and file transfer to download file, but I don't know how to do it?
Please point me to any hint. Thanks!
Here might solve your problem:
var downloadUrl = encodeURI(cordova.file.dataDirectory + fileName);
var hostUrl = encodeURI(serverUrl);
var fileTransfer = new FileTransfer();
fileTransfer.download(
hostUrl,
downloadUrl,
function(entry) {
alert('Your download has completed.');
},
function(error) {
alert(error.source);
},
false,
{
headers: {
"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
}
}
);
You might refer to the solution in here. If you need further information, please elaborate.
First you have to install cordova File and FileTransfer plugins. After that, you call the following function to download file.
function filetransfer(download_link, filepath) {
var fileTransfer = new FileTransfer();
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
fileTransfer
.download(
download_link,
fileSystem.root.toURL() + '/' + filepath,
function(entry) {
alert("Download completed");
}, function(error) {
alert("upload error code" + error.code);
});
});
Install plugins
cordova plugin add cordova-plugin-file
cordova plugin add cordova-plugin-file-transfer
now use them.
Cordova-file-transfer
Methods
upload: Sends a file to a server.
download: Downloads a file from server.
abort: Aborts an in-progress transfer.
Cordova-file
Methods (Not all)
createFile(path, file, replace): Creates a new file in the specific path. The replace boolean value determines whether to replace an existing file with the same name. If an existing file exists and the replace value is false, the promise will fail and return an error.
removeFile(path, file): Removes a file from a desired location.
writeFile(path, file, data, replace): Write to a new file, and replace if desired.
moveFile(path, file, newPath, newFile): Read a file in various methods.
copyFile(path, file, newPath, newFile): Read a file in various methods. If file exists, will fail to copy.