sion委托函数totalBytesExpectedToWrit

sion委托函数totalBytesExpectedToWrit

本文介绍了URLSession委托函数totalBytesExpectedToWrite始终返回-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了用于下载文件的URLSession,文件正在正确下载,没问题.

I have created URLSession for downloading a file, File is being downloaded correctly no problem with that.

我想显示剩余字节的百分比计数,但是要使用委托函数:

I want to show the percentage count of remain bytes, but the delegate function:

-(void) URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite

及其参数 totalBytesExpectedToWrite 总是返回-1.

几天前一切正常,代码没有变化,但是突然停止发送期望的字节.

All thing was working fine before few days, there is no change with code but it suddenly stopped sending Expected bytes.

我的请求代码如下:

NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];

NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];

NSMutableURLRequest*request = [NSMutableURLRequest requestWithURL:fileUrl];

NSDictionary*param = [[NSDictionary alloc]initWithObjectsAndKeys:@"",@"Accept-Encoding", nil];
[request setAllHTTPHeaderFields:param];

NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request];
[downloadTask resume];

我缺少的api是否有变化?还是其他方式?

is there any change with the api which i am missing? OR any other way around?

推荐答案

-1 NSURLSessionTransferSizeUnknown ,这意味着http服务器未提供"Content-Length""标头(然后使用传输编码:分块"发送数据.)

-1 is NSURLSessionTransferSizeUnknown, which means that the http server did not provide a "Content-Length" header (and the data is sent using "Transfer-Encoding: chunked").

您可能无能为力.您也可以尝试 https://stackoverflow.com/a/12599242/1187415 中的解决方法是否也适用于您的情况:

There is probably not much that you can do. You could try if the workaround from https://stackoverflow.com/a/12599242/1187415 works in your case as well:

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

这篇关于URLSession委托函数totalBytesExpectedToWrite始终返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 23:05