Ios NSURLSessionDownloadTask中totalBytesExpectedToWrite为-1
我面临一个奇怪的问题。我使用Ios NSURLSessionDownloadTask中totalBytesExpectedToWrite为-1,ios,nsurlsession,Ios,Nsurlsession,我面临一个奇怪的问题。我使用NSURLSession和NSURLSessionDownloadTask从Internet加载文件。这是密码 NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfiguration:kSessionId]; self.session = [NSURLSession sessionWithConfiguration:sessi
NSURLSession
和NSURLSessionDownloadTask
从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
,我无法向用户显示进度,因为我不知道下载文件的大小
您能提示我哪里出错吗?
-1
是NSURLSessionTransferSizeUnknown
,这意味着http服务器没有提供
“内容长度”标题(数据使用“传输编码:分块”发送)
你能做的可能不多。如果您的案例中的解决方法也有效,您可以尝试:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:anURL];
[request addValue:@"" forHTTPHeaderField:@"Accept-Encoding"];
web服务可能未在标题字段内容长度中提供总大小 如果未提供总大小,应用程序将无法知道长度,这将提供一个进度条
使用诸如Charles Proxy之类的分析器检查来自web服务器的内容。内容长度可以是非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
//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(@"=========");
}
因此,您需要检查downloadTask.response中的内容长度
2015-10-27 16:04:18.580 ClarksonsDirectory[89873:15495901] downloadTask.response:<NSHTTPURLResponse: 0x7f9eabaae750> { 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%]
2015-10-27 16:04:18.580克拉克森目录[89873:15495901]下载任务。响应:{URL:http://asset10232:50/api/1/dataexport/ios/?lastUpdatedDate=01012014000000 }{状态代码:200,标题{
“缓存控制”=“无缓存”;
“内容处置”=“附件;文件名=目录.zip”;
“内容编码”=gzip;
“内容长度”=33666264;
“内容类型”=“应用程序/八位字节流”;
日期=“2015年10月27日星期二16:03:55 GMT”;
Expires=“-1”;
Pragma=“无缓存”;
Server=“Microsoft IIS/8.5”;
“X-AspNet-Version”=“4.0.30319”;
“X-Powered-By”=“ASP.NET”;
} }
ContentLength字符串:[33666264]
字节:47278
写入的字节总数:33606690
totalBytesExpectedToWrite:-1
下载百分比:[99.823045%]
可能是服务器问题,它没有正确发送内容长度
标题。如果在浏览器中使用相同的URL,浏览器是否显示正确的进度?