tags:

views:

250

answers:

3

hi all

i want to download a file from a server using API as i send a request the content on that link comes to my document directory in iphone/ipod touch, and after downloading is it possible to remove them from the document directory. is there any way to do the things like that .

Thanks Balraj

+2  A: 

Easiest way is to use ASIHTTPRequest and do:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDownloadDestinationPath:@"{path to file in documents folder}"]];
[request startAsynchronous];

...

- (void)requestFinished:(ASIHTTPRequest *)request 
{
 // Downloaded file is ready for use
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
 // Download failed. This is why.
 NSError *error = [request error];
}
Ramin
is this the iphone API if is it in which framework it is ?
balraj
ASIHTTPRequest. The link is in the text above the code.
Ramin
+3  A: 
NSData *data = [NSData dataWithContentsOfURL:@"http://site.com/filename"];

NSArray  *docList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir  = [docList objectAtIndex:0];
NSString *documentPath = [historicDocumentDir stringByAppendingPathComponent:@"filename"];

[data writeToFile:documentPath atomically:NO];

Add error checking.

zaph
+3  A: 

To remove a file from your documents folder use:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
mahboudz