29,028
社区成员
发帖
与我相关
我的任务
分享
- (void) downloadFileWithUrl: (NSString*)fileUrl {
NSString *filePath = fileUrl;
NSString *fileHost ;
fileHost = HOST_ADDRESS;
NSString *tempPath = NSTemporaryDirectory();
localFilePath = [NSString stringWithFormat:@"%@%@.pdf",tempPath,filePath];
[self clearParameter];
[self setDownLoadUrl:filePath];
[self downloadToFile:localFilePath];//设置存放路径的地址
[self setShowWaiting:YES];
}
FileDownloadNetworkAction *fileDownLoad = [FileDownloadNetworkAction new];
[fileDownLoad downloadFileWithUrl:filePath];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSDictionary *cellFileData = [[[[fileFormData getFileFormGroupArray] objectAtIndex:indexPath.section] valueForKey:@"groupData"] objectAtIndex:indexPath.row];
NSString *url = [cellFileData valueForKey:@"pdfUrl"];
pdfUrl = url;
if ([[cellFileData valueForKey:@"prefix"] isEqualToString:@"正文"] || [[cellFileData valueForKey:@"prefix"] isEqualToString:@"处理单"] || [[cellFileData valueForKey:@"prefix"] isEqualToString:@"稿纸"]) {
fileTitle = [self.fileData valueForKey:@"title"];
if (!fileTitle) {
fileTitle = [self.fileData valueForKey:@"name"];
}
}else{
fileTitle = [NSString stringWithFormat:@"%@",[cellFileData valueForKey:@"title"]];
}
if ([[pdfUrl lastPathComponent] isEqualToString:@"null"]){
[self.rootVC showHudText:@"文件打开失败"];
return;
}
if (isIntegrated){
pdfUrl = [YXFramework chanegPath:pdfUrl newIp:[[NSUserDefaults standardUserDefaults] valueForKey:@"Server_IP"]];
}
[self downloadFileURL:pdfUrl savePath:NSTemporaryDirectory() fileName:[NSString stringWithFormat:@"%@%@",[cellFileData valueForKey:@"title"],@".pdf"] withTitle:fileTitle];
//
}
- (void)downloadFileURL:(NSString *)aUrl savePath:(NSString *)aSavePath fileName:(NSString *)aFileName withTitle:(NSString*)title
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *fileName = [NSString stringWithFormat:@"%@%@", aSavePath, [aUrl lastPathComponent]];
if ([fileManager fileExistsAtPath:fileName]) {
[self.rootVC showFile:fileName title:title];
}else{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
hud.mode = MBProgressHUDModeAnnularDeterminate;
hud.labelText = @"下载中";
if (![fileManager fileExistsAtPath:aSavePath]) {
[fileManager createDirectoryAtPath:aSavePath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSURL *url = [[NSURL alloc] initWithString:aUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.inputStream = [NSInputStream inputStreamWithURL:url];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:fileName append:NO];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"is download:%f", (float)totalBytesRead/totalBytesExpectedToRead);
hud.progress = (float)totalBytesRead/totalBytesExpectedToRead;
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
DLog(@"下载成功");
[hud hide:YES];
NSString *filePath = [NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), [aUrl lastPathComponent]];
//[self performSelector:@selector(showFile:) withObject:[NSArray arrayWithObjects:filePath,title, nil] afterDelay:0.5];
[self.rootVC showFile:filePath title:title];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DLog(@"下载失败");
[self showHudText:@"下载失败"];
[hud hide:YES];
}];
[operation start];
}
}