setDownloadProgressBlock中的totalBytesExpectedToRead保持为-1,直到下载完成 [英] totalBytesExpectedToRead in setDownloadProgressBlock remains -1 until download is done
问题描述
我一直在谷歌搜索无效,希望对此有帮助。
我尝试使用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屋!