Download url file to cordova app error

I am making an application that should download images from my website and store them on my phone, but when I try the phone book, I see all the errors that may occur. What can I do to fix this = /?

var fileTransfer = new FileTransfer(); fileTransfer.download( "http://developer.android.com/assets/images/home/ics-android.png", "/", function(entry) { alert("download complete: " + entry.fullPath); }, function(error) { alert("download error source " + error.source); alert("download error target " + error.target); alert("upload error code" + error.code); }); 

Errors shown:

 Download error source " the url used" download error target: " the target used " upload error code 1 

I am using cordova 2.2.0

Here is the logcat error log:

  12-06 09:07:26.413: E/FileTransfer(2186): {"target":"\/","source":"http:\/\/developer.android.com\/assets\/images\/home\/ics-android.png","code":1} 12-06 09:07:26.413: E/FileTransfer(2186): java.io.FileNotFoundException 12-06 09:07:26.413: E/FileTransfer(2186): at org.apache.cordova.FileTransfer.getFileFromPath(FileTransfer.java:794) 12-06 09:07:26.413: E/FileTransfer(2186): at org.apache.cordova.FileTransfer.access$700(FileTransfer.java:62) 12-06 09:07:26.413: E/FileTransfer(2186): at org.apache.cordova.FileTransfer$4.run(FileTransfer.java:631) 12-06 09:07:26.413: E/FileTransfer(2186): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 12-06 09:07:26.413: E/FileTransfer(2186): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 12-06 09:07:26.413: E/FileTransfer(2186): at java.lang.Thread.run(Thread.java:856) 
+4
source share
2 answers

You are so close, it is just your target file, which is incorrect. Try:

 var fileTransfer = new FileTransfer(); fileTransfer.download( "http://developer.android.com/assets/images/home/ics-android.png", "file://sdcard/ics-android.png", function(entry) { alert("download complete: " + entry.fullPath); }, function(error) { alert("download error source " + error.source); alert("download error target " + error.target); alert("upload error code" + error.code); }); 

You need the prefix "file: //" and you cannot save it to "/" since you do not have permissions.

+8
source

I tried this and this work in Android, but I have not tested it in ios.And yet, the download was successful, since I use it in my project.

 enter code here function fun(){ var dfd = $.Deferred(function (dfd){ var remoteFile = "Your link"; var localFileName = remoteFile.substring(remoteFile.lastIndexOf('/')+1); window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) { fileSystem.root.getFile(localFileName, {create: true, exclusive: false}, function(fileEntry) { var localPath = fileEntry.fullPath; if (device.platform === "Android" && localPath.indexOf("file://") === 0) { localPath = localPath.substring(7); }// You need to write IOS instead of Android var ft = new FileTransfer(); ft.download(remoteFile, localPath, function(entry) { dfd.resolve('file downloaded'); // Do what you want with successful file downloaded and then // call the method again to get the next file //downloadFile(); }, fail); }, fail); }, fail); }); return dfd.promise(); } fun().then(function(msg){ if(msg==="file downloaded") { alert("Download complete"); } else { alert("Download error") } }); function fail(){ alert("error"); } 
+2
source

All Articles