PhoneGap Plugin for downloading URL

Have you considered using Appcelerator for your project? File downloads are pretty straightforward plus there are plenty more advantages to running a NATIVE app versus running your app in a web client. Contact us for more information on Appcelerator and why it is the platform we use for all solutions.

 of a Plugin for downloading images or videos to local iOS device, this is based on the PhoneGap Plugin API.

Basically provide a URL and a file name and the contents will be saved to the local documents folder of the application. If you notice, the download is happening in a seperate thread to that the UI will continue to respond

// call to excute the download in a background thread
-(void) downloadFileFromUrl:(NSMutableArray*)paramArray
	NSLog(@"in PixFileDownload.downloadFileFromUrl",nil);
	[self performSelectorInBackground: @selector( downloadFileFromUrlInBackgroundTask ) withObject: paramArray waitUntilDone:NO];

// downloads the file in the background and saves it to the local documents
// directory for the application
-(void) downloadFileFromUrlInBackgroundTask:(NSMutableArray*)paramArray
	NSLog(@"in PixFileDownload.downloadFileFromUrlInBackgroundTask",nil);
	NSString * sourceUrl = [paramArray objectAtIndex:0];
	NSString * fileName = [paramArray objectAtIndex:1];

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSData* theData = [NSData dataWithContentsOfURL: [NSURL URLWithString:sourceUrl] ];

	// save file in documents directory
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];

	NSString *newFilePath = [documentsDirectory stringByAppendingString:fileName];

	NSFileManager *fileManager=[NSFileManager defaultManager];
	NSError *error=[[[NSError alloc]init] autorelease];

	BOOL response = [theData writeToFile:newFilePath options:NSDataWritingFileProtectionNone error:&error];

	NSLog(@"file save result %@", response);

    // send our results back to the main thread
    [self performSelectorOnMainThread:@selector(downloadComplete:)
						   withObject:nil waitUntilDone:NO];

	[pool drain];

// calls the predefined callback in the ui to indicate completion
-(void) downloadComplete {
	NSLog(@"in PixFileDownload.downloadComplete",nil);
	NSString * jsCallBack = [NSString stringWithFormat:@"pixFileDownloadComplete('%@');",@"return message to ui"];
	[webView stringByEvaluatingJavaScriptFromString:jsCallBack];

The javascript code

function PixFileDownload() {

PixFileDownload.prototype.downloadFile = function(url,destFileName) {
    PhoneGap.exec("PixFileDownload.downloadFile", url,destFileName);

PhoneGap.addConstructor(function() {
	window.fileDownloadMgr = new PixFileDownload();

function  pixFileDownloadComplete( filePath ) {
      alert( "Success r" + filePath );

function  pixFileDownloadCompleteWithError( message ) {
    alert( "Error r" + message );

2 Comments on “PhoneGap Plugin for downloading URL

  1. This is really good work! but ı can’t find download place, I must download in WWW file, can you give any sample to download ?

Leave a Reply

Your email address will not be published. Required fields are marked *

eight − 2 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>