NSURLSessionDownloadTask中的totalBytesExpectedToWrite为-1

我遇到了一个奇怪的问题。 我使用NSURLSessionNSURLSessionDownloadTask从Internet加载文件。 这是代码

 NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:kSessionId]; self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:[NSOperationQueue new]]; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSURLSessionDownloadTask *downloadTask = [self.session downloadTaskWithRequest:request]; [downloadTask resume]; 

我的类被声明为NSURLSessionDownloadDelegate ,我得到了很好的回调。 但是当系统调用委托方法时

 - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { NSLog(@"totalBytesExpectedToWrite: %lld", totalBytesExpectedToWrite); NSLog(@"%lld", totalBytesWritten); } 

totalBytesExpectedToWrite始终等于-1 ,我无法向用户显示进度,因为我不知道下载文件的大小。

你能告诉我哪里弄错了吗?

-1NSURLSessionTransferSizeUnknown ,这意味着http服务器没有提供“Content-Length”标头(并且使用“Transfer-Encoding:chunked”发送数据)。

您可以做的事情可能不多。 您可以尝试https://stackoverflow.com/a/12599242/1187415的解决方法也适用于您的情况:

 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:anURL]; [request addValue:@"" forHTTPHeaderField:@"Accept-Encoding"]; 

Web服务可能未在标题字段Content-Length中提供总大小。

如果未提供总大小,则您的应用无法知道长度,这会提供进度条。

使用Charles Proxy等分析器检查Web服务器的内容。

Content-Length可以是非0和totalBytesExpectedToWrite:-1

 //TRACK PROGRESS - MOVED DOWN as also used in BACKGROUND REFRESH > DOWNLOAD FILE > CALL DELEGATE -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { //to see response header NSLog(@"downloadTask.response:%@\n", downloadTask.response); // { status code: 200, headers { // "Cache-Control" = "no-cache"; // "Content-Disposition" = "attachment; filename=Directory.zip"; // "Content-Encoding" = gzip; // "Content-Length" = 33666264; // "Content-Type" = "application/octet-stream"; // Date = "Tue, 27 Oct 2015 15:50:01 GMT"; // Expires = "-1"; // Pragma = "no-cache"; // Server = "Microsoft-IIS/8.5"; // "X-AspNet-Version" = "4.0.30319"; // "X-Powered-By" = "ASP.NET"; // } } NSDictionary *responseHeaders = ((NSHTTPURLResponse *)downloadTask.response).allHeaderFields; NSString * contentLengthString = responseHeaders[@"Content-Length"]; double contentLengthDouble = 0.0f; if (contentLengthString) { NSNumberFormatter *f = [[NSNumberFormatter alloc] init]; NSNumber *contentLengthNumber = [f numberFromString:contentLengthString]; contentLengthDouble = [contentLengthNumber doubleValue]; }else{ } NSLog(@"contentLengthString:[%@]", contentLengthString); //You can get progress her NSLog(@"bytesWritten:%lld", bytesWritten); NSLog(@"totalBytesWritten:%lld", totalBytesWritten); //DONT USE CAN BE ALWAYS -1 for Gzip NSLog(@"totalBytesExpectedToWrite:%lld", totalBytesExpectedToWrite); //avoid DIV by 0 if (contentLengthDouble > 0.0) { double percentage1 = (totalBytesWritten / contentLengthDouble); double percentage = percentage1 * 100.0; NSLog(@"PERCENTAGE DOWNLOADED:[%f%%]", percentage); }else{ NSLog(@"PERCENTAGE DOWNLOADED:[contentLengthDouble is 0]"); } NSLog(@"========="); } 

以下是在下载zip时反复输出。

但是totalBytesExpectedToWrite:-1

所以你需要在downloadTask.response中检查Content-Length

 2015-10-27 16:04:18.580 ClarksonsDirectory[89873:15495901] downloadTask.response: { URL: http://asset10232:50/api/1/dataexport/ios/?lastUpdatedDate=01012014000000 } { status code: 200, headers { "Cache-Control" = "no-cache"; "Content-Disposition" = "attachment; filename=Directory.zip"; "Content-Encoding" = gzip; "Content-Length" = 33666264; "Content-Type" = "application/octet-stream"; Date = "Tue, 27 Oct 2015 16:03:55 GMT"; Expires = "-1"; Pragma = "no-cache"; Server = "Microsoft-IIS/8.5"; "X-AspNet-Version" = "4.0.30319"; "X-Powered-By" = "ASP.NET"; } } contentLengthString:[33666264] bytesWritten:47278 totalBytesWritten:33606690 totalBytesExpectedToWrite:-1 PERCENTAGE DOWNLOADED:[99.823045%]