Friday, December 30, 2011

On the Sixth Day of PhoneGapping: Downloading a File

Did you know that the FileTransfer object got a new download method in PhoneGap 1.3.0? You didn't, well thanks to Alexander Heinrich for spurring it's  development and getting it checked in for Android and iOS (download is also available to you BlackBerry users). Finally PhoneGap developers have a convenient way to download binary files.

The method signature is:
download(source, destination, successCallback, errorCallback);
Where the parameters are:

source: The URL you want to download
destination: The full file path you want the file stored at
successCallback: called with a FileEntry object that represents the new file
errorCallback: called when something goes wrong

So say we want to download a PNG image and store it on the SD card. Well you'd write some code that looks like this:

7 comments:

GIACOPINO said...

Hi,

I've juste copy/paste your sample code, but it doesn't work

I'm running phonegap 1.3.0

2012-01-05 20:17:05.842 FileAssets[37881:13403] [INFO] >>> onDeviceReady
2012-01-05 20:17:05.843 FileAssets[37881:13403] File Transfer downloading file...
2012-01-05 20:17:06.943 FileAssets[37881:18a03] Write file /ics.png
2012-01-05 20:17:06.945 FileAssets[37881:13403] File Transfer Error: http://developer.android.com/assets/images/home/ics-android.png
2012-01-05 20:17:06.948 FileAssets[37881:13403] [INFO] download error source http://developer.android.com/assets/images/home/ics-android.png
2012-01-05 20:17:06.948 FileAssets[37881:13403] [INFO] download error target /ics.png
2012-01-05 20:17:06.949 FileAssets[37881:13403] [INFO] upload error code2

Can you help me?

Simon Mac Donald said...

@GIACOPINO

Do you have an SD Card? Do you have write external down as one of your permissions in your AndroidManifest.xml?

GIACOPINO said...

Hi simon

I'm on IOs...

Simon Mac Donald said...

Right so if you just copy and paste my code you won't have a "/sdcard". You should remove that bit from the sample and try again.

GIACOPINO said...

Hi again this is what i've done is you look at the output :

2012-01-05 20:17:06.943 FileAssets[37881:18a03] Write file /ics.png

I've also whitelist all url by "*"

I've tried :

path = '/ics.png' and path='ics.png' and always the same error

Tks for your help

Simon Mac Donald said...

I'm not really the iOS expert. You should come over to the PhoneGap Google Group and ask your question.

GIACOPINO said...

Done :
http://groups.google.com/group/phonegap/browse_thread/thread/7c3674b92587c9d0