Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. document.getElementById("myBtn").addEventListener("click", function () {
  2. download("http://cordova.apache.org/static/img/cordova_bot.png", "data", "new_file");
  3. });
  4.  
  5. function download(URL, Folder_Name, File_Name) {
  6. window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail);
  7.  
  8. function fileSystemSuccess(fileSystem) {
  9. var download_link = encodeURI(URL);
  10. ext = download_link.substr(download_link.lastIndexOf('.') + 1); //Get extension of URL
  11.  
  12. var directoryEntry = fileSystem.root; // to get root path of directory
  13. directoryEntry.getDirectory(Folder_Name, {
  14. create: true,
  15. exclusive: false
  16. }, onDirectorySuccess, onDirectoryFail); // creating folder in sdcard
  17. var rootdir = fileSystem.root;
  18. var fp = rootdir.toURL();
  19. fp = fp + "/" + Folder_Name + "/" + File_Name + "." + ext; // fullpath and name of the file which we want to give
  20. filetransfer(download_link, fp);
  21. }
  22.  
  23. function onDirectorySuccess(parent) {
  24. // Directory created successfuly
  25. }
  26.  
  27. function onDirectoryFail(error) {
  28. alert("Unable to create new directory: " + error.code);
  29. }
  30.  
  31. function fileSystemFail(evt) {
  32. //Unable to access file system
  33. alert(evt.target.error.code);
  34. }
  35. }
  36.  
  37. function filetransfer(download_link, fp) {
  38. var fileTransfer = new FileTransfer();
  39. fileTransfer.download(download_link, fp,
  40. function (entry) {
  41. alert("download complete: " + entry.fullPath);
  42. //cordova.plugins.imagesaver.saveImageToGallery(entry.fullPath, successCallback, errorCallback);
  43. },
  44. function (error) {
  45. alert("download error source " + error.source);
  46. }
  47. );
  48. }
  49.  
  50. function download() {
  51. window.requestFileSystem(window.TEMPORARY, 5 * 1024 * 1024, function (fs) {
  52.  
  53. var url = 'http://cordova.apache.org/static/img/cordova_bot.png';
  54. fs.root.getFile('downloaded-image.png', {
  55. create: true,
  56. exclusive: false
  57. }, function (fileEntry) {
  58. file_transfer(fileEntry, encodeURI(url), true);
  59.  
  60. }, onErrorCreateFile);
  61.  
  62. }, onErrorLoadFs);
  63. }
  64.  
  65. function onErrorLoadFs(msg){
  66. alert(msg);
  67. }
  68.  
  69. function onErrorCreateFile(msg){
  70. alert(msg);
  71. }
  72.  
  73. function file_transfer(fileEntry, uri, readBinaryData) {
  74.  
  75. var fileTransfer = new FileTransfer();
  76. var fileURL = fileEntry.toURL();
  77.  
  78. fileTransfer.download(
  79. uri,
  80. fileURL,
  81. function (entry) {
  82. alert("download complete: " + entry.toURL());
  83.  
  84. if (readBinaryData) {
  85. // Read the file...
  86. readBinaryFile(entry);
  87. } else {
  88. // Or just display it.
  89. displayImageByFileURL(entry);
  90. }
  91.  
  92. },
  93. function (error) {
  94. alert("download error source " + error.source);
  95. alert("download error target " + error.target);
  96. alert("upload error code" + error.code);
  97. },
  98. null, // or, pass false
  99. {
  100. //headers: {
  101. // "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
  102. //}
  103. }
  104. );
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement