If you want to save image from url, you have to merge the following code to you js file
First of all, we have to add the following plug-in to read, write and navigate file system.
Write this in your code:
cordova plugin add org.apache.cordova.file
cordova plugin ls
Secondly, we have to check the parameters mismatch:
function DownloadFile(URL, Folder_Name, File_Name) {
if (URL == null && Folder_Name == null && File_Name == null) {
return;
} else {
download(URL, Folder_Name, File_Name);
}
}
Thirdly, we have to "Write permission" and "Folder Creation":
function download(URL, Folder_Name, File_Name) {
// Request the file system
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail);
function fileSystemSuccess(fileSystem) {
var download_link = encodeURI(URL);
ext = download_link.substr(download_link.lastIndexOf('.') + 1); //Get extension of URL
var directoryEntry = fileSystem.root; // For root path of directory
directoryEntry.getDirectory(Folder_Name, { create: true, exclusive: false }, onDirectorySuccess, onDirectoryFail); // creating folder in sdcard
var rootdir = fileSystem.root;
var fp = rootdir.fullPath; // Gives Fullpath of local directory
fp = fp + "/" + Folder_Name + "/" + File_Name + "." + ext; // fullpath and name of the file which we want to give
// Function call to download
filetransfer(download_link, fp);
}
function onDirectorySuccess(parent) {
// Directory successfuly created
}
function onDirectoryFail(error) {
// On error
alert("Unable to create new directory: " + error.code);
}
function fileSystemFail(evt) {
//Unable to access file system
alert(evt.target.error.code);
}
}
Fourthly, We have to download a file into created folder:
function filetransfer(download_link, fp) {
var fileTransfer = new FileTransfer();
// Local path and File download function with URL
fileTransfer.download(download_link, fp,
function (entry) {
alert("download complete: " + entry.fullPath);
},
function (error) {
// Failed errors
alert("download error source " + error.source);
}
);
}
1 Comment(s)