4

I want to download pdf file. When I download small pdf file then I get plus value but when i download large file then I get minus value using afnetworking.

here is my code:

- (IBAction)download:(id)sender {


    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@""]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    NSString *pdfName = @"The_PDF_Name_I_Want.pdf";

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:pdfName];
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
    [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {

        NSLog(@"Download = %f", (float)totalBytesRead / totalBytesExpectedToRead);
                self.progressView3.progress = (float)totalBytesRead / totalBytesExpectedToRead;

    }];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Successfully downloaded file to %@", path);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

    [operation start];
}

see my output

2016-04-06 15:04:53.842 pdf[8149:60b] Download = -811521.000000
2016-04-06 15:04:53.849 pdf[8149:60b] Download = -817179.000000
2016-04-06 15:04:53.860 pdf[8149:60b] Download = -819123.000000
2016-04-06 15:04:53.872 pdf[8149:60b] Download = -823469.000000
2016-04-06 15:04:53.879 pdf[8149:60b] Download = -826393.000000
2016-04-06 15:04:53.921 pdf[8149:60b] Download = -827820.000000
2016-04-06 15:04:53.932 pdf[8149:60b] Download = -830744.000000
2016-04-06 15:04:53.939 pdf[8149:60b] Download = -833662.000000

please solve my problem...

Vinod Vishwanath
  • 5,821
  • 2
  • 26
  • 40
Jigar
  • 1,801
  • 1
  • 14
  • 29
  • What are the values (`totalBytesRead` and `totalBytesExpectedToRead`)? One could be negative, which one? Or is a cast issue. – Larme Apr 06 '16 at 10:01
  • here this negative value is (float)totalBytesRead / totalBytesExpectedToRead – Jigar Apr 06 '16 at 10:03
  • I know, I understood the logs you put, but considering each values, is one of them negative? As float ? As long long ? – Larme Apr 06 '16 at 10:05
  • Which ONE of `totalBytesRead` and `totalBytesExpectedToRead` is negative? The numerator or the denominator? – Vinod Vishwanath Apr 06 '16 at 10:05
  • wait bro i will check it – Jigar Apr 06 '16 at 10:07
  • see response 2016-04-06 15:39:28.643 pdf[8222:60b] Download = -16077.000000 2016-04-06 15:39:28.649 pdf[8222:60b] total bytesread16077.000000 2016-04-06 15:39:28.651 pdf[8222:60b] total bytesexpected-1 – Jigar Apr 06 '16 at 10:10

2 Answers2

1

As your logs show, the totalBytesExpectedToRead is -1. This happens when the Content-Length isn't provided in the response headers.

You can handle this either by asking the server-side guys to return the appropriate Content-Length, or in the iOS app by showing a simple Activity Indicator instead of a Progress View.

Vinod Vishwanath
  • 5,821
  • 2
  • 26
  • 40
1

your problem is content length to upload pdf file in server .totalBytesExpectedToRead is -1 if the Content-Length HTTP header isn't provided

by the server.you should either add this header to your server, or handle -1 by showing a UIActivityIndicator instead of a UIProgressView.

dany
  • 100
  • 8