-
-
Save dhavaln/3055240 to your computer and use it in GitHub Desktop.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | |
<title>Insert title here</title> | |
<script type="text/javascript" src="lib/android/cordova-1.7.0.js"></script> | |
<script type="text/javascript"> | |
window.appRootDirName = "download_test"; | |
document.addEventListener("deviceready", onDeviceReady, false); | |
function onDeviceReady() { | |
console.log("device is ready"); | |
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); | |
} | |
function fail() { | |
console.log("failed to get filesystem"); | |
} | |
function gotFS(fileSystem) { | |
console.log("filesystem got"); | |
window.fileSystem = fileSystem; | |
fileSystem.root.getDirectory(window.appRootDirName, { | |
create : true, | |
exclusive : false | |
}, dirReady, fail); | |
} | |
function dirReady(entry) { | |
window.appRootDir = entry; | |
console.log("application dir is ready"); | |
} | |
downloadFile = function(){ | |
var fileTransfer = new FileTransfer(); | |
var url = "http://www.irs.gov/pub/irs-pdf/fw4.pdf"; | |
var filePath = window.appRootDir.fullPath + "/test.pdf"; | |
fileTransfer.download( | |
url, | |
filePath, | |
function(entry) { | |
alert("download complete: " + entry.fullPath); | |
}, | |
function(error) { | |
alert("download error" + error.source); | |
} | |
); | |
} | |
</script> | |
</head> | |
<body> | |
<a href="#" onclick="downloadFile()">Download File</a> | |
</body> | |
</html> |
Which Phonegap version you are using??
cordova-1.8.1.js
Tested and the code works without any changes in all three 1.7.0, 1.8.1 and 1.9.0 versions.
Whitelist entry for 1.8.1 and 1.9.0 is:
<access origin="http://www.irs.gov*" />
Ok thanks..let me try..
does it also work?
Boss..
Getting errors...
07-11 13:10:37.904: E/FileTransfer(383): {"target":"file:///mnt/sdcard/download_test/test.pdf","source":"http://www.irs.gov/pub/irs-pdf/fw4.pdf","code":3}
07-11 13:10:37.904: E/FileTransfer(383): java.net.UnknownHostException: www.irs.gov
07-11 13:10:37.904: E/FileTransfer(383): at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
07-11 13:10:37.904: E/FileTransfer(383): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
07-11 13:10:37.904: E/FileTransfer(383): at java.net.InetAddress.getAllByName(InetAddress.java:256)
07-11 13:10:37.904: E/FileTransfer(383): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.(HttpConnection.java:69)
07-11 13:10:37.904: E/FileTransfer(383): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.(HttpConnection.java:48)
07-11 13:10:37.904: E/FileTransfer(383): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
07-11 13:10:37.904: E/FileTransfer(383): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
07-11 13:10:37.904: E/FileTransfer(383): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
07-11 13:10:37.904: E/FileTransfer(383): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
07-11 13:10:37.904: E/FileTransfer(383): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
07-11 13:10:37.904: E/FileTransfer(383): at org.apache.cordova.FileTransfer.download(FileTransfer.java:463)
07-11 13:10:37.904: E/FileTransfer(383): at org.apache.cordova.FileTransfer.execute(FileTransfer.java:88)
07-11 13:10:37.904: E/FileTransfer(383): at org.apache.cordova.api.PluginManager$1.run(PluginManager.java:186)
07-11 13:10:37.904: E/FileTransfer(383): at java.lang.Thread.run(Thread.java:1019)
:( tried both < access origin="._"> and
Is your emulator connected to internet?? the above error is not related to code but rather a network issue
ok.setting proxy..
Now, Internet is working.but still getting some errors like--
07-11 14:49:16.585: E/FileTransfer(548): {"target":"file:///data/data/org.apache.demo/download_test/test.pdf","source":"http://www.irs.gov/pub/irs-pdf/fw4.pdf","http_status":407,"code":1}
errors as follows
07-11 14:49:16.585: E/FileTransfer(548): java.io.FileNotFoundException: http://www.irs.gov/pub/irs-pdf/fw4.pdf
07-11 14:49:16.585: E/FileTransfer(548): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
07-11 14:49:16.585: E/FileTransfer(548): at org.apache.cordova.FileTransfer.download(FileTransfer.java:467)
07-11 14:49:16.585: E/FileTransfer(548): at org.apache.cordova.FileTransfer.execute(FileTransfer.java:88)
07-11 14:49:16.585: E/FileTransfer(548): at org.apache.cordova.api.PluginManager$1.run(PluginManager.java:186)
07-11 14:49:16.585: E/FileTransfer(548): at java.lang.Thread.run(Thread.java:856)
Hi Dhaval,
Thanks for ur help.. : ) I agree,no errors in code.
Do u hav any idea? How to tackle above errors...pls help if u knw.
again thanks ..
http error code 407 is for the proxy authentication, is your proxy requires authentication??
By default android mobile browser has access to proxy and auth but app uses WebView which has access to only proxy and not auth.
http://stackoverflow.com/questions/11106769/http-proxy-in-android-emulator-works-for-browser-not-for-webview
Try the emulator options to give the proxy info at the time of starting emulator and it might work for you - developer.android.com/tools/help/emulator.html#startup-options
Dhaval,actually,proxy requires authentication. both values are set. internet is working.
still getting errors like file not found etc etc...
Hi Dhava, does this work with Cordova 2.0.. I am getting the error- "downloaderror -and the url" Any help will be appreciated
Yes. This isn't working in Cordova 2.0
My bad. Works like a charm.
Just one minor thing tho': I dont see a reason why a folder has to be created again, given that the files are already stored inside the sandboxed application folder.
Where is that FileTransfer class coming from? Is that JS or native code?
D'oh. Please ignore last. (It's an object in the File API that I just hadn't noticed before.)
Hi Dhaval...
Given code is not working..Giving error @ url : "http://www.irs.gov/pub/irs-pdf/fw4.pdf";
Not getting..will u pls help for the same.
I am using android 4.0.3.(Testing it on emulator)
Thanks in advance...!