IOS开发 文件下载 文件预览

来源:互联网 发布:数据库的基本知识 编辑:程序博客网 时间:2024/05/18 00:51



-(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
原创粉丝点击