首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSURLSessionDownloadTask中的totalBytesExpectedToWrite为-1

NSURLSessionDownloadTask中的totalBytesExpectedToWrite为-1
EN

Stack Overflow用户
提问于 2014-05-11 03:34:03
回答 3查看 3.6K关注 0票数 7

我遇到了一个奇怪的问题。我使用NSURLSessionNSURLSessionDownloadTask从互联网上加载文件。以下是代码

代码语言:javascript
复制
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,并且我得到了很好的回调。但是当系统调用委托方法时

代码语言:javascript
复制
- (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,我没有能力向用户显示进度,因为我不知道下载文件的大小。

你能提示我哪里弄错了吗?

EN

回答 3

Stack Overflow用户

发布于 2014-05-11 03:49:56

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

你能做的可能不多。如果https://stackoverflow.com/a/12599242/1187415的变通方法也适用于您的情况,您可以尝试:

代码语言:javascript
复制
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:anURL];
[request addValue:@"" forHTTPHeaderField:@"Accept-Encoding"];
票数 12
EN

Stack Overflow用户

发布于 2014-05-11 03:52:35

web服务可能没有在报头字段Content-Length中提供总大小。

如果没有提供总大小,你的应用程序就无法知道长度,这会提供一个进度条。

使用Charles Proxy之类的分析器检查来自web服务器的内容。

票数 2
EN

Stack Overflow用户

发布于 2015-10-28 00:09:43

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

代码语言:javascript
复制
//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中的内容长度

代码语言:javascript
复制
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%]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23585432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档