其他分享
首页 > 其他分享> > iOS 少数doc,html文件在线无法打开,支持在线下载并打开

iOS 少数doc,html文件在线无法打开,支持在线下载并打开

作者:互联网

//下载文件
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer.timeoutInterval = 60;
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSString * URL=[self.DocDownUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[manager GET:URL parameters:@"" progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

  // 获取数据流,写入本地
     [self writeToFile:responseObject fileName:nameStr];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                //"%@",error);
 }];

//写入文件并打开
-(void)writeToFile:(NSData *)data fileName:(NSString *)fileName{
    
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * _filePath = [paths objectAtIndex:0];
    fileName = [NSString stringWithFormat:@"%@",fileName];
    NSString * filePath = [_filePath stringByAppendingPathComponent:fileName];
    NSURL * fileUrl=[NSURL URLWithString:@""];
    // NSData写入文件
    [data writeToFile:filePath atomically:YES];
    //NSLog(@"fileDataPath is %@", filePath);
    if (@available(iOS 9.0, *)) {
        [self.wkWebView loadFileURL:fileUrl allowingReadAccessToURL:[NSURL fileURLWithPath:filePath]];
    }
}

标签:在线,filePath,doc,self,NSString,fileName,NSURL,manager,打开
来源: https://blog.csdn.net/lfl18326162160/article/details/121035817