2016-04-06 24 views
4

Pdf dosyasını indirmek istiyorum. Küçük pdf dosyası indirdiğimde artı değer elde ederim ama büyük dosya indirdiğimde afnetworking kullanarak eksi değer elde ediyorum. totalBytesExpectedToRead olduğunu Olumsuz değer Progress Block Dosya indirme sırasında

- (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]; 
} 

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 

benim sorunu çözmek lütfen benim çıkışını ... loglarınız gösterdiği gibi

+0

Değerler ("totalBytesRead" ve "totalBytesExpectedToRead") nedir? Biri olumsuz olabilir, hangisi? Ya da döküm bir sorundur. – Larme

+0

burada bu negatif değer (float) toplamBytesRead/totalBytesExpectedToRead – iOS

+0

Biliyorum, koyduğunuz günlükleri anladım, ama her bir değeri göz önünde bulundurarak bunlardan biri negatif mi? Şamandıra olarak mı? Uzun süre mi? – Larme

cevap

1

senin sorunun sunucusunda pdf dosyası yüklemek için içerik uzunluğudur .totalBytesExpectedToRead -1 İçerik-Uzunluk HTTP başlık sunucunuza bu başlığı eklemek gerekir ya server.You tarafından

sağlanmazsa, veya UIProgressView yerine bir UIActivityIndicator görüntüleyerek -1 işlemek.

1

bkz -1:

burada benim kodudur. Bu, yanıt başlıklarında Content-Length sağlanmadığında gerçekleşir.

Bunu, sunucu tarafındaki adamlardan uygun İçerik Uzunluğunu döndürmesini isteyerek veya iOS uygulamasında İlerleme Görünümü yerine basit bir Etkinlik Göstergesi göstererek yapabilirsiniz.