Naveen
Naveen

Reputation: 1251

How do I get the file size to know whether it's download or not?

I am using following code for downloading epub /pdf from a url .I like to give a progress bar so when I start downloading it shows the progress and when the download completes it will popup a message. How can I implement this?

My code for downloading file

-(void)Download
 {
    NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL    URLWithString:@"http://www.feedbooks.com/book/3471.epub"]];

    //Store the Data locally as epub  File if u want pdf change the file extension  

    NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];

    NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"3471.epub"];

    [pdfData writeToFile:filePath atomically:YES];
    NSLog(@"%@",filePath);
 }

I am using this codes in my .m file but it's not working for me

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    _totalFileSize = response.expectedContentLength;
    responseData = [[NSMutableData alloc] init];
}


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    _receivedDataBytes += [data length];
    MyProgressBar.progress = _receivedDataBytes / (float)_totalFileSize;
    [responseData appendData:data];
}

Upvotes: 1

Views: 1375

Answers (3)

Manu
Manu

Reputation: 4750

use NSURLConnection

in .h file

double datalength;
NSMutableData *databuffer;
UIProgressView *progress;

in .m file

-(void)Download
{
      NSURLConnection *con=[[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.feedbooks.com/book/3471.epub"]] delegate:self startImmediately:YES];
      [con start];
}

delegate methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    datalength = [response expectedContentLength];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [databuffer appendData:data];
    progress.progress = (databuffer.length/datalength);
    self.HUD.detailsLabelText = [NSString stringWithFormat:@"Downloading  %.f  %%",(databuffer.length/datalength)*100];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
    NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"3471.epub"];
    [pdfData writeToFile:filePath atomically:YES];
    NSLog(@"%@",filePath);
}

Upvotes: 2

Nirav Jain
Nirav Jain

Reputation: 5107

You can check data length of NSData. Then you will find actual downloaded data.

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
{
    // append the new data to the receivedData

    [receivedData appendData:data];
}

Here you will get length of data in bytes. You can convert it as per your need.

It may help you.

Upvotes: 0

NG.
NG.

Reputation: 22914

If you do curl -vvv -o epub.pdf http://www.feedbooks.com/book/3471.epub you will see the following line:

Content-Length: 603244

The content-length header is the size in bytes of the data you are downloading. You can use that to track your progress as you write your data.

With your current code, you can't really do what you want though. You should check out this answer for more info.

Upvotes: 0

Related Questions