Kha*_*inn 2 objective-c ios afnetworking afhttprequestoperation
我试图从iPad使用afnetworking从服务器下载文件.
我可以成功下载文件.但是,奇怪的是,虽然totalBytesRead是正确的,但我的totalBytesExpectedToRead总是-1.我哪里弄错了?
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead)
{
}
Run Code Online (Sandbox Code Playgroud)
整个代码在这里.
-(void)fileDownload : (NSString *)pathToDownload : (NSString *)fileExt
{
hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeAnnularDeterminate;
hud.labelText=@"Downloading...";
NSLog(@"fileDownload and pathToDownload is %@",pathToDownload);
NSArray *parts = [pathToDownload componentsSeparatedByString:@"\\"];
NSString *fileName = [NSString stringWithFormat:@"%@",[parts objectAtIndex:[parts count]-1]];
if([fileExt isEqualToString:@"pdf"])
{
fileNameForAllThreeFile=[fileName stringByDeletingPathExtension];
}
NSLog(@"fileName is %@",fileName);
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IP_ADDRESS,download_recording]];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient setAuthorizationHeaderWithUsername:usernameForAuth password:passwordForAuth];
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
pathToDownload,@"file",
nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:download_recording parameters:parameters];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileName];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Request Successful in fileDownload, response '%@'", responseStr);
NSLog(@"Successfully downloaded file to %@", path);
[self messageDisplay:@"File download complete" :[NSString stringWithFormat:@"%@ download is now complete.",fileName]];
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error in retrieveFromCustomServer: %@", error.localizedDescription);
[self performSelectorInBackground:@selector(hideHUDInBackground) withObject:nil];
}];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead)
{
//do something in this line with the calculation to cell
// float progress = (float)totalBytesRead / totalBytesExpectedToRead;
NSLog(@"Downloaded %lld of %lld bytes", totalBytesRead, totalBytesExpectedToRead);
float perCent = totalBytesRead / (float)totalBytesExpectedToRead;
hud.progress = perCent ;
NSLog(@"Percent is %d and in float is %f",(int)(perCent *100),perCent);
}];
[operation start];
}
Run Code Online (Sandbox Code Playgroud)
totalBytesExpectedToRead如果Content-Length服务器未提供HTTP标头,则为-1 .参见RFC 2616,第14.13节.
您应该将此标头添加到服务器,或通过显示UIActivityIndicator而不是UIProgressView来处理-1.
或者,在您的情况下,使用MBProgressHUD:
hud.mode = MBProgressHUDModeIndeterminate;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
902 次 |
| 最近记录: |