xcode - setDownloadProgressBlock中的totalBytesExpectedToRead保持为-1,直到下载完成

标签 xcode download afnetworking

我一直在搜寻无济于事,并希望您对此有所帮助。

我尝试使用AFHTTPRequestOperation流(通过设置outputstream)下载图像。
它将下载文件,没问题。
但是进度条不会显示正确,因为totalBytesExpectedToRead始终返回-1,并且仅在下载完成后才返回正确的值。

这是流媒体的本质吗?
还是我做错了什么?

我的代码如下。

提前致谢!

(void)invokeAsynchronousSTREAMING:(NSString *)path locationToSave:(NSString *)locationToSave参数:(NSDictionary *)paramDict callId:(NSString *)callId {

NSMutableURLRequest *request = [[AFServerAPIClient sharedClient] requestWithMethod:@"GET" 
                                                                              path:path 
                                                                        parameters:paramDict];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

if(locationToSave!=nil) {
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:locationToSave append:NO];
}

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)  {

   //DO SOMETHING

} 
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //DO SOMETHING
    }
 ];

[operation setDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {

    NSLog(@"invokeAsyncronousSTREAMING - Received %lld of %lld bytes", totalBytesRead, totalBytesExpectedToRead);

    //DO SOMETHING
}];

[operation start];

}//结束invokeAsyncStreaming

最佳答案

如果服务器未在响应中设置-1 HTTP header ,则下载进度可能会返回Content-Length

在这种情况下,建议您改用不确定的进度指示器。

关于xcode - setDownloadProgressBlock中的totalBytesExpectedToRead保持为-1,直到下载完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13042925/

相关文章:

php - 下载 .zip 文件运行损坏的文件 php

python 以 : urllib2 ClientForm 形式下载文件

ios - 在 Swift 的 UITableViewCell 中同步滚动 UICollectionViews

swift - 如何使用 applicationWillResignActive 调用 View Controller 的函数

c# - 可恢复文件下载问题 -> 当文件大小超过 Int32.MaxValue(2GB)(大文件)时

ios - AFNetworking/NSURLConnection HTTPS keep alive 显示奇怪的行为

php - 字典中的 JSON 编码字典未使用 AFNetworking 正确反序列化

ios - 使用 AFNetworking 从 S3 获取公共(public)文件并接收 403

ios - Entitlements.plist 与 Appname.Entitlements

iphone - 在查看 View 时更新 UITableView?