文件上传及进度显示
2015-11-20 17:00
211 查看
之前遇到需要显示文件上传时的进度问题,就整理了下资料,没什么复杂的东西,看代码吧。
NSString *urlString = [NSString stringWithFormat:@"%@api/?method=gdb.send_talk",BASEURL];//你的URL NSDictionary *dict = @{@"type":[NSString stringWithFormat:@"%d",pvtype],@"content":_textView.text};//需要传的dict,没有写nil AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer]; NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:urlString parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // 设置时间格式 formatter.dateFormat = @"yyyyMMddHHmmss"; NSString *str = [formatter stringFromDate:[NSDate date]]; //按时间给传的文件命名 NSString *imageName = [NSString stringWithFormat:@"%@.jpg", str]; NSString *videoName = [NSString stringWithFormat:@"%@.mp4", str]; [formData appendPartWithFileData:videoData name:@"video" fileName:videoName mimeType:@"mp4"];//传视频 videoData 视频所在的data [formData appendPartWithFileData:image_data name:@"image" fileName:imageName mimeType:@"jpg"];//传图片 imageData 图片所在的data }]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success %@", responseObject); [self.navigationController popViewControllerAnimated:YES]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Failure %@", error.description); }]; [operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { NSLog(@"传了%.2fMB/共 %.2fMB",totalBytesWritten/1024.0/1024.0,totalBytesExpectedToWrite/1024.0/1024.0); }]; [operation start];
相关文章推荐
- PHP连接mysql
- Centos6.6 本地 yum 配置
- Service not registered
- LightOJ 1051 - Good or Bad (dp)
- 小P的故事——神奇的换零钱
- linux设备驱动归纳总结(一):内核的相关基础概念
- ListView使用完全讲解
- DFS入门:全排列算法及POJ 1564 Sum it up详解
- uva112
- 写文件,并且规定文件的权限
- android studio 教程
- 从学员角度体验教练技术之考驾照
- sublime常用快捷方式
- openfire日志配置
- 关于Tableview 方法insertRowsAtIndexPaths 报错问题
- BeautifulSoup应用笔记(2)- 遍历文档树
- 滑动选项导航卡SlidingTabLayout和SlidingTabStrip(系列1)
- 小P的故事——神奇的分组
- java 中的异常和错误
- js获取radio选中的值