How to save downloaded data to device's filesystem - iphone

I'm downloading pdf documents from the server:
- (void)downloadSingleDocument:(NSURL *)url
{
[pdfData release];
pdfData = [[NSMutableData alloc] init];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req addValue:#"Basic **************=" forHTTPHeaderField:#"Authorization"];
downloadConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
}
- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data
{
NSLog(#"Connection did receive data");
[pdfData appendData:data];
}
On connectionDidFinishLoading I want to save downloaded pdf file to filesystem in Documents directory with the same filename as it was on the server.
What is the best way to do it?

Use this methods if you have large file:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response
{
filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:save_name];
[[NSFileManager defaultManager] createFileAtPath:filepath contents:nil attributes:nil];
file = [[NSFileHandle fileHandleForUpdatingAtPath:filepath] retain];// Here file is object of NSFileHandle and its declare in .h File
[file seekToEndOfFile];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[file seekToEndOfFile];
[file writeData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
[file closeFile];
// After you download your data. you can copy your data from here to filesystem. and remove from here
}

Use NSURLDownload.
Sorry, that is only available on the Mac, not for iOS.
Create a temporary file, and append the received data to it in connection:didReceiveData:. In connectionDidFinishLoading:, move the file to the correct place, and in connection:didFailWithError:, remove the temporary file.

This should do it
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSString *filename = [con.originalRequest.URL lastPathComponent];
NSString *path = [documentsDirectory stringByAppendingString:filename];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:path contents:pdfData attributes:nil];
}

Related

Write file to NSDocument directory show UIProgressView in iPhone?

I have a pdf file from server.
Then I need to load the file to NSDocument directory path and its working fine, but i want to show UIProgressView for store each bytes. How to do this , please help me
Thanks in Advance
I tried to store like this:
NSError *error;
NSArray *ipaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *idocumentsDir = [ipaths objectAtIndex:0];
NSString *idataPath = [idocumentsDir stringByAppendingPathComponent:#"File"];
NSLog(#"idataPath:%#",idataPath);
//Create folder here
if (![[NSFileManager defaultManager] fileExistsAtPath:idataPath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:idataPath withIntermediateDirectories:NO attributes:nil error:&error];
}
// Image Download here
NSString *fileName = [idataPath stringByAppendingFormat:#"/image.jpg"];
NSLog(#"imagePathDOWNLOAD:%#",fileName);
NSData *pdfData1 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[URLArray objectAtIndex:a]]];
[pdfData1 writeToFile:fileName atomically:YES];
You can use NSURLConnection class to implement progress bar:
.h:
NSMutableData *responseData;
NSString *originalFileSize;
NSString *downloadedFileSize;
.m:
- (void)load {
NSURL *myURL = [NSURL URLWithString:#""];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
responseData = [[NSMutableData alloc] init];
originalFileSize = [response expectedContentLength];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
downloadedFileSize = [responseData length];
progressBar.progress = ([originalFileSize floatValue] / [downloadedFileSize floatValue]);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[responseData release];
[connection release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"Succeeded! Received %d bytes of data",[responseData lengtn]);
}
Let me know for any queries.

downloading using NSURLConnection not downloading anything?

i am using NSURLConnection to download mp3 data from the server , my code is here
- (IBAction)downloadData:(id)sender
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSURL *url = [[NSURL alloc] initWithString:#"http://viadj.viastreaming.net/start/psalmsmedia/ondemand/Nin%20snehamethrayo.mp3"];
[request setURL:url];
[url release];
url = nil;
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[responseData release];
[connection release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"Succeeded! Received %d bytes of data",[responseData
length]);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"myFile"];
[responseData writeToFile:fileName atomically:YES];
responseData = nil;
self->imageConnection = nil;
}
am little bit confused about the path given to download. when i click download button i shows "Succeeded! Received 1329 bytes of data" but nothing is downloading. need some help. how will we specify the local path of iPhone to store downloaded data?
- (IBAction)downloadData:(id)sender
{
NSURL *url = [[NSURL alloc] initWithString:#"http://viadj.viastreaming.net/start/psalmsmedia/ondemand/Nin%20snehamethrayo.mp3"];
NSMutableURLRequest *theRequest_to = [NSMutableURLRequest requestWithURL:url];
[url release];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:theRequest_to delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response
{
NSString *filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"snehamethrayo.mp3"]; // here you can set your filename which you can get from url
[[NSFileManager defaultManager] createFileAtPath:filepath contents:nil attributes:nil];
file = [[NSFileHandle fileHandleForUpdatingAtPath:filepath] retain];// Here file is object of NSFileHandle and its declare in .h File
[file seekToEndOfFile];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[file seekToEndOfFile];
[file writeData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
[file closeFile];
}
No need for any code change I think.Just put an nslog and see...
NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"myFile"];
NSLog(#"%#",fileName);
That will list the file location like this
/Users/me/Library/Application Support/iPhone Simulator/5.0/Applications/(your app)/Documents/myFile. ie the downloaded file is in your document folder.
note: don't forget to put the file format ie
NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"myFile.mp3"];

NSMutableData Save to a File

I downloaed the file using code shown below. Then i am trying to save NSMutableData variable to file, however, the file is not created. What am i doing wrong? Do i need to convert NSMutableData into NSString?
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)_response {
response = [_response retain];
if([response expectedContentLength] < 1) {
data = [[NSMutableData alloc] init];
}
else {
data = [[NSMutableData dataWithCapacity:[response expectedContentLength]] retain];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)_data {
[data appendData:_data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"file" ofType:#"txt"];
NSLog(#"saved: %#", filePath);
[data writeToFile:filePath atomically:YES];
NSLog(#"downloaded file: %#", data); //all i see in log is some encoded data here
}
You can’t write inside your app’s bundle. You’ll need to save it somewhere else, like your app’s Documents directory:
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"file.txt"];
[data writeToFile:filePath atomically:YES];

how to avoid overwrite file

i am using NSURLConnection to download file from server and storing it locally as follows
-(void) connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
{
[webData appendData:data];
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
self.documentsDir = [[paths objectAtIndex:0]stringByAppendingPathComponent:#"NewResult.zip" ];
[[NSFileManager defaultManager] createFileAtPath:documentsDir contents:nil attributes:nil];
NSFileHandle *file1 = [NSFileHandle fileHandleForUpdatingAtPath: documentsDir];
[file1 writeData: webData];
[file1 closeFile];
}
this is working but overwrite old file every time
how to avoid this over writing I know this is possible with NSURLDownload but here i am not using NSURLDownload.
if (![fileManager fileExistsAtPath: path]){
NSLog(#"File not exists at path %#", path);
}else{
NSLog(#"File exists at path:%#", path);
}

How to download a large file with the iPhone SDK and avoid memory usage issues?

I'm using the NSURLConnection class to download a large file in my iPhone application, but it crashes every so often because it's using too much memory. I'm doing the usual NSURLConnection usage, to append the received data to a NSMutableData object.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.fileData appendData:data];
}
Then after I finish downloading the whole file, I save it to a local temporary file, and read it as a mapped file like this:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// save the downloaded data into a temporary file
NSString *tempPath = NSTemporaryDirectory();
NSString *tempFile = [tempPath stringByAppendingPathComponent:#"temp.pdf"];
[self.fileData writeToFile:tempFile atomically:YES];
NSData *mappedData = [NSData dataWithContentsOfMappedFile:tempFile];
NSURL *baseURL = [NSURL URLWithString:#"http://mydomain.com"];
[webView loadData:mappedData MIMEType:#"application/pdf" textEncodingName:#"UTF-8" baseURL:baseURL];
}
What can I improve here to avoid these memory usage problems?
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response {
filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:save_name]; // filename is in .h file
[[NSFileManager defaultManager] createFileAtPath:filename contents:nil attributes:nil];
file =
[[NSFileHandle fileHandleForUpdatingAtPath:filename] retain];// file is in .h
//if (file) {
//
// [file seekToEndOfFile];
// }
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSD
ata *)data {
if (file) {
[file seekToEndOfFile];
} [file writeData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection*)connection {
[file closeFile];
}
If it's that large, why not write it to the file as it comes in, rather than keeping it in an NSData object?
i'm using
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:save_name];
NSFileHandle *file1 = [NSFileHandle fileHandleForUpdatingAtPath: filename];
[file1 writeData: data];
[file1 closeFile];
}