IOS开发 文件下载 文件预览



-(void)downloadFile:(NSString *)UrlAddress andWith:(NSString *)styleName11 

{

//UrlAddress 是下载链接 styleName11 是文件名+格式

    NSURLRequest *request = [NSURLRequestrequestWithURL:[NSURLURLWithString:UrlAddress]];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperationalloc]initWithRequest:request];

//获取在当前程序中NSDocumentDirectory文件目录

    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

//拼接上文件名 就是文件将要下载到 Document 的地址

    NSString *path = [[pathsobjectAtIndex:0]stringByAppendingPathComponent:[NSStringstringWithFormat:@"%@",styleName11]];

    operation.outputStream = [NSOutputStreamoutputStreamToFileAtPath:pathappend:NO];

    

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id responseObject) {




    } failure:^(AFHTTPRequestOperation *operation,NSError *error) {

        NSLog(@"Error: %@", error);

    }];

    [operation setDownloadProgressBlock:^(NSUInteger bytesRead,longlong totalBytesRead,longlong totalBytesExpectedToRead) {

        

        NSLog(@"Download = %f", (float)totalBytesRead / totalBytesExpectedToRead); // 下载进度


        

    }];

    [operation start];

}


//打开下载的文件 IOS有自带的文本预览的类  

UIDocumentInteractionController

//这个类平常的办公文件格式都可以打开 比如 jpg png txt doc pdf 

NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

//获取下载到 NSDocumentDirectory下的文件 

                NSString *path = [NSStringstringWithFormat:@"%@/%@",[pathsobjectAtIndex:0],@"text.doc"];

                //但是 如果直接打开图片的话 会显示图片信息 但是不显示图片内容  打开txt格式的话 会乱码 所以在打开之前进行编码 调用下面的方法

                [selftransformEncodingFromFilePath:path];

//编码成功之后 再打开

                _documentInteraction = [UIDocumentInteractionControllerinteractionControllerWithURL:[NSURLfileURLWithPath:path]];

                [_documentInteraction setDelegate:self];

                [_documentInteractionpresentPreviewAnimated:YES];



//编码方法

#pragma mark - 在打开之前进行编码否则 text文件会乱码

 - (void)transformEncodingFromFilePath:(NSString *)filePath{

     //调用上述转码方法获取正常字符串

     NSString *body = [selfexamineTheFilePathStr:filePath];

     //转换为二进制

     NSData *data = [bodydataUsingEncoding:NSUTF16StringEncoding];    //覆盖原来的文件

     [data writeToFile:filePath atomically:YES];     //此时在读取该文件,就是正常格式啦

 }

- (NSString *)examineTheFilePathStr:(NSString *)str{

    NSStringEncoding *useEncodeing = nil;     //带编码头的如utf-8等,这里会识别出来

    NSString *body = [NSStringstringWithContentsOfFile:strusedEncoding:useEncodeingerror:nil];

    //识别不到,按GBK编码再解码一次.这里不能先按GB18030解码,否则会出现整个文档无换行bug

    if (!body) {

        body = [NSStringstringWithContentsOfFile:strencoding:0x80000632error:nil];

       

    }     //还是识别不到,按GB18030编码再解码一次.

    if (!body) {

        body = [NSStringstringWithContentsOfFile:strencoding:0x80000631error:nil];

    }

    return body;

}

//代理方法需要加上

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller

{

    return self;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值