Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- document.getElementById("myBtn").addEventListener("click", function () {
- download("http://cordova.apache.org/static/img/cordova_bot.png", "data", "new_file");
- });
- function download(URL, Folder_Name, File_Name) {
- 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; // to get 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.toURL();
- fp = fp + "/" + Folder_Name + "/" + File_Name + "." + ext; // fullpath and name of the file which we want to give
- filetransfer(download_link, fp);
- }
- function onDirectorySuccess(parent) {
- // Directory created successfuly
- }
- function onDirectoryFail(error) {
- alert("Unable to create new directory: " + error.code);
- }
- function fileSystemFail(evt) {
- //Unable to access file system
- alert(evt.target.error.code);
- }
- }
- function filetransfer(download_link, fp) {
- var fileTransfer = new FileTransfer();
- fileTransfer.download(download_link, fp,
- function (entry) {
- alert("download complete: " + entry.fullPath);
- //cordova.plugins.imagesaver.saveImageToGallery(entry.fullPath, successCallback, errorCallback);
- },
- function (error) {
- alert("download error source " + error.source);
- }
- );
- }
- function download() {
- window.requestFileSystem(window.TEMPORARY, 5 * 1024 * 1024, function (fs) {
- var url = 'http://cordova.apache.org/static/img/cordova_bot.png';
- fs.root.getFile('downloaded-image.png', {
- create: true,
- exclusive: false
- }, function (fileEntry) {
- file_transfer(fileEntry, encodeURI(url), true);
- }, onErrorCreateFile);
- }, onErrorLoadFs);
- }
- function onErrorLoadFs(msg){
- alert(msg);
- }
- function onErrorCreateFile(msg){
- alert(msg);
- }
- function file_transfer(fileEntry, uri, readBinaryData) {
- var fileTransfer = new FileTransfer();
- var fileURL = fileEntry.toURL();
- fileTransfer.download(
- uri,
- fileURL,
- function (entry) {
- alert("download complete: " + entry.toURL());
- if (readBinaryData) {
- // Read the file...
- readBinaryFile(entry);
- } else {
- // Or just display it.
- displayImageByFileURL(entry);
- }
- },
- function (error) {
- alert("download error source " + error.source);
- alert("download error target " + error.target);
- alert("upload error code" + error.code);
- },
- null, // or, pass false
- {
- //headers: {
- // "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
- //}
- }
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement