#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger, QHFileType) {
QHFileTypePlan, //企业计划书
QHFileTypeMoney,//企业财务
QHFileTypeAsset//企业资产
};
@interface QHDownLoadManager : NSObject
- (void)downloadwithURLstr:(NSString *)urlStr projectId:(NSString *)projectId andType:(QHFileType)fileType progressHandler:(void(^)(float percent))progressHandler complete:(void(^)(BOOL isSuccess,NSString *locaPath))complete;
@end
#import <AFNetworking/AFNetworking.h>
@implementation QHDownLoadManager
- (void)downloadwithURLstr:(NSString *)urlStr projectId:(NSString *)projectId andType:(QHFileType)fileType progressHandler:(void(^)(float percent))progressHandler complete:(void(^)(BOOL isSuccess,NSString *locaPath))complete{
NSString *fileName;
switch (fileType) {
case QHFileTypePlan:
{
fileName = [NSString stringWithFormat:@"%@Plan",projectId];
}
break;
case QHFileTypeMoney:
{
fileName = [NSString stringWithFormat:@"%@Money",projectId];
}
break;
case QHFileTypeAsset:
{
fileName = [NSString stringWithFormat:@"%@Asset",projectId];
}
break;
default:
break;
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", KURLQINIUGetImageTwo, urlStr]];
fileName = [NSString stringWithFormat:@"%@%@",fileName,[url lastPathComponent]];
NSString *filePath = [self documentsPath:fileName];
if ([fileManager fileExistsAtPath:filePath]) {
if (complete) {
complete(YES,filePath);
}
}else{
// NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KURLQINIUGetImageTwo,urlStr]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
if (progressHandler) {
progressHandler( downloadProgress.completedUnitCount);
}
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
return [NSURL fileURLWithPath:filePath];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
if (complete) {
complete(error?NO:YES,[filePath path]);
}
}];
[task resume];
}
}
- (NSString *)documentsPath:(NSString *)fileName{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths lastObject];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
return filePath;
}
@end
使用:
#pragma mark 文件预览下载
- (void)previewFileClicked:(NSString *)title {
NSString *fileURL;
QHFileType type = QHFileTypePlan;
if ([title isEqualToString:QHLocalizedString(@"商业计划书", nil)]) {
fileURL = _icoModel.businessPlan;
type = QHFileTypePlan;
}else if ([title isEqualToString:QHLocalizedString(@"企业财务报表", nil)]) {
fileURL = _icoModel.financial;
type = QHFileTypeMoney;
}else if ([title isEqualToString:QHLocalizedString(@"企业资产评估报告", nil)]) {
fileURL = _icoModel.appraisalReport;
type = QHFileTypeAsset;
}
[[[QHDownLoadManager alloc]init] downloadwithURLstr:fileURL projectId:_icoModel.recordId andType:type progressHandler:^(float percent) {
dispatch_async(dispatch_get_main_queue(), ^{
[self showProgress:percent];
});
} complete:^(BOOL isSuccess, NSString *locaPath) {
if (isSuccess) {
[MBProgressHUD hideHUDForView:self.view animated:YES];
[self openDocxWithPath:locaPath];
}else{
[MBProgressHUD showMessag:QHLocalizedString(@"上传失败", nil) toView:self.view afterDelay:hiddenMBPTime];
}
}];
}
- (void)showProgress:(CGFloat)progress{
MBProgressHUD *hud = [MBProgressHUD HUDForView:self.view];
if(!hud){
hud = [[MBProgressHUD alloc]initWithView:self.view];
hud.mode = MBProgressHUDModeAnnularDeterminate;
hud.removeFromSuperViewOnHide = YES;
[self.view addSubview:hud];
[hud show:YES];
}
hud.progress = progress;
}
-(void)openDocxWithPath:(NSString *)filePath{
// QHDocViewCtrl *doc = [[QHDocViewCtrl alloc]initWithNibName:@"QHDocViewCtrl" bundle:nil];
// doc.docPath = filePath;
// [self.navigationController pushViewController:doc animated:YES];
NSURL *urlPath = [NSURL fileURLWithPath:filePath];
UIDocumentInteractionController *doc= [UIDocumentInteractionController interactionControllerWithURL:urlPath];
doc.delegate = self;
doc.name = QHLocalizedString(@"文件", nil);
[doc presentPreviewAnimated:YES];
}
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller {
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller {
return self.view.frame;
}