setDownloadProgressBlock中的totalBytesExpectedToRead保持为-1,直到下载完成 [英] totalBytesExpectedToRead in setDownloadProgressBlock remains -1 until download is done

查看:406
本文介绍了setDownloadProgressBlock中的totalBytesExpectedToRead保持为-1,直到下载完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在谷歌搜索无效,希望对此有帮助。



我尝试使用AFHTTPRequestOperation流(通过设置输出流)下载图像。
它下载文件,没问题。
但是进度条不会显示正确,因为totalBytesExpectedToRead总是返回-1,下载完成后只返回正确的值。



流?
或者我做错了?



我的代码如下。



提前感谢! (NSString *)locationToSave参数:(NSDictionary *)paramDict callId:(NSString *)callId {// / p>

(void)invokeAsynchronousSTREAMING

  NSMutableURLRequest * request = [[AFServerAPIClient sharedClient] requestWithMethod:@GET
path:path
参数: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
}
];

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

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

// DO SOMETHING
}];

[operation start];

} // end invokeAsyncStreaming

解决方案

如果服务器未设置 Content-Length -1 >响应中的HTTP头。



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


I have been googling to no avail, and hope for your help on this.

I tried to download an image using AFHTTPRequestOperation streaming (by setting outputstream). It downloads the file, no problem. But the progress bar wont display correct, because totalBytesExpectedToRead always returns -1, and only returns the correct value when download is complete.

Is this the nature of streaming? Or did I do something wrong?

My code below.

Thanks in advance!

(void)invokeAsynchronousSTREAMING:(NSString*)path locationToSave:(NSString*)locationToSave parameters:(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];

}//end invokeAsyncStreaming

解决方案

Download progress may return -1 if the server does not set the Content-Length HTTP header in the response.

In this case, it is recommended that you use an indeterminate progress indicator instead.

这篇关于setDownloadProgressBlock中的totalBytesExpectedToRead保持为-1,直到下载完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆