NSURLConnection上传,进度条
2016-07-03 17:35
253 查看
结合上一篇下载NSURLConnection下载大小文件,断点下载
本片介绍上传,显示进度条
本片介绍上传,显示进度条
- (IBAction)showImgPicker:(UIButton *)sender { //UIImagePickerController 图片选择器 UIImagePickerController *imgPicker = [[UIImagePickerController alloc]init ]; //allowsEditing 是否允许编辑 imgPicker.allowsEditing = YES; //isSourceTypeAvailable 设置图片来源,默认为相册,可以设置为摄像头 if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { NSLog(@"摄像头不可用"); } else { imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera; } imgPicker.delegate = self; [self presentViewController:imgPicker animated:YES completion:nil]; [imgPicker release]; } /** * 选择完成图片时调用 */ - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo { _imgView.image = image; [self dismissViewControllerAnimated:YES completion:nil]; } //上传图片 - (IBAction)UploadImageClick:(UIButton *)sender { NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:8080/FileUploadServer/UploadServlet"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //上传文件必须用POST请求 [request setHTTPMethod:@"POST"]; //forHTTPHeaderField 设置请求头 Content-type,请求体中的数据格式 [request addValue:@"multipart/form-data" forHTTPHeaderField:@"Content-type"]; //UIImageJPEGRepresentation 把一个UIImage转成jpg格式的数据,第二个参数是压缩比例,范围0~1 //NSData *data = UIImageJPEGRepresentation(_imgView.image, 0.7); //UIImagePNGRepresentation 把一个UIImage转成png格式的数据 NSData *data = UIImagePNGRepresentation(_imgView.image); //把图片数据放入请求体 [request setHTTPBody:data]; NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; [connection start]; } //上传大文件 - (IBAction)uploadFile:(UIButton *)sender { NSString *path = [[NSBundle mainBundle]pathForResource:@"file" ofType:@"zip"]; //NSInputStream 输入流 ,用于大量数据的输入 //inputStreamWithFileAtPath 创建一个文件的输入流 NSInputStream *stream = [NSInputStream inputStreamWithFileAtPath:path]; NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:8080/FileUploadServer/UploadServlet"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //上传文件必须用POST请求 [request setHTTPMethod:@"POST"]; //设置请求头 Content-type,请求体中的数据格式 [request addValue:@"multipart/form-data" forHTTPHeaderField:@"Content-type"]; //获得上传文件大小 NSDictionary *attDic = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:nil]; [request addValue:[NSString stringWithFormat:@"%lld",[attDic fileSize]] forHTTPHeaderField:@"Content-Length"]; //setHTTPBodyStream 设置请求体的输入流,当上传的数据量很大时,无法全部读入内粗,就需要用输入就上传 [request setHTTPBodyStream:stream]; NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; [connection start]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"请求失败:%@",error); } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {} - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {} - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"上传完毕!"); } //当发送出数据时调用 - (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite { //NSLog(@"%ld----%ld----%ld",bytesWritten,totalBytesWritten,totalBytesExpectedToWrite); _progressView.progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite; }
相关文章推荐
- android自定义加载旋转框
- TSP问题之模拟退火算法
- springmvc表单的时间转换
- 我的音乐播放器(2) 服务控制音乐的状态
- arcgis api for js共享干货系列之二自定义Navigation控件样式风格
- LaTeX修改字体颜色
- 2016.07.03砸题选讲(Worldwide_D)小结
- PERCONA-TOOLKIT : pt-ioprofile分析IO情况
- 文件上传
- Java中Integer、int、String类型之间的转换关系
- SSH error:a public key file has not been specified by this session
- ServiceLoader跟DriverManager使用总结
- mdd的烦恼
- 易經大意(凸紫色) p201-300
- 序言
- MySQL基本命令
- NSURLConnection下载大小文件,断点下载
- Java序列化
- iOS7、8、9相册适配
- 通过ServiceLoader实现链式处理