AFNetworking2.0 AFHTTPRequestOperationManager实现上传、下传
来源:互联网 发布:ubuntu字体渲染 编辑:程序博客网 时间:2024/06/14 21:18
AFHTTPRequestOperationManager实现在任意格式数据下载,网上查找只有上传,下载图片的资料。
1、上传示例
AFHTTPRequestOperationManager *requestManager = [[AFHTTPRequestOperationManager alloc] init]; requestManager.responseSerializer = [AFJSONResponseSerializer serializer]; [requestManager POST:@"http://192.168.165.68:8080/xxxxx/fileController/upload.action" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"时钟.zip" ofType:nil]]; NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:@"multipart/form-data" forKey:@"Content-Type"]; [dictionary setObject:[NSNumber numberWithInteger:data.length] forKey:@"Content-Length"]; [dictionary setObject:@"form-data; name=\"f2\"; filename=\"时钟.zip\"" forKey:@"Content-Disposition"]; [formData appendPartWithHeaders:dictionary body:data]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { // 服务器返回JSON的数据 NSLog(@"content = %@", [responseObject objectForKey:@"content"]); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error = %@", error); }];
根据实际情况进行修改,服务器返回数据的时候,需设置response的ContentType设置为"text/json;charset=UTF-8。否则success块将不会被调用。
2、下载示例
AFHTTPRequestOperationManager *requestManager = [[AFHTTPRequestOperationManager alloc] init]; requestManager.responseSerializer = [AFHTTPResponseSerializer serializer]; [requestManager POST:@"http://192.168.165.68:8080/xxxx/fileController/download.action" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSDictionary *headers = operation.response.allHeaderFields; // 解析下载文件名 NSString *content = [headers objectForKey:@"Content-Disposition"]; NSString *fileName = [[content componentsSeparatedByString:@"="] lastObject]; // 保存文件 NSString *destPath = [NSString stringWithFormat:@"%@/tmp/%@", NSHomeDirectory(), [fileName lastPathComponent]]; [operation.responseData writeToFile:destPath atomically:YES]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error = %@", error); }];
0 0
- AFNetworking2.0 AFHTTPRequestOperationManager实现上传、下传
- NetWorking 之AFHTTPRequestOperationManager上传图像
- AFHTTPRequestOperationManager
- AFNetworking2.0
- AFNetWorking2.0
- AFHTTPRequestOperationManager注意点 上传图片到服务器
- AFNetworking2.0 NSHipster翻译
- AFNetworking2.0 NSHipster翻译
- AFNetworking2.0源码解析
- AFNetworking2.0源码解析
- AFNetworking2.0源码解析
- AFNetworking2.0 NSHipster翻译
- AFnetworking2.0翻译
- IOS AFNetworking2.0 问题
- AFnetworking2.0翻译
- AFNetworking2.0源码解析
- AFNetworking2.0源码解析
- AFNetworking2.0 NSHipster翻译
- 从设计模式看抽象类和接口的区别
- Android Handler机制
- hdoj.2068 RPG的错排 20140807
- 【转】动窗口在storm中的实现
- 自动化安全工具已经成为信息安全领域
- AFNetworking2.0 AFHTTPRequestOperationManager实现上传、下传
- lucene.net 搜索引擎
- PCL 在windows 下的配置(不需要Cmake)
- 整理Git的命令使用
- Public Function ConnectString
- [HDOJ 4900] NO ACM NO LIFE [树的直径]
- 干货,设计即体验
- lua中类的属性是table时,多个类的实例使用了同一个table属性
- Mysql的rpm的安装(原)