NSURLConnection上传,进度条
来源:互联网 发布:山西软件行业协会 编辑:程序博客网 时间:2024/05/21 11:23
结合上一篇下载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;}
7 0
- NSURLConnection上传,进度条
- iOS NSURLCONNECTION下载进度条
- NSURLConnection文件上传
- NSURLConnection文件上传&&解压缩
- IOS NSURLConnection 文件上传
- IOS使用NSURLConnection实现文件上传
- NSURLConnection(苹果原生上传单文件)
- iOS开发 使用NSURLConnection实现图片上传
- NSURLConnection
- NSURLConnection
- NSURLConnection
- NSURLConnection
- NSURLConnection
- NSURLConnection
- NSURLConnection
- NSURLConnection
- NSURLConnection
- NSURLConnection
- 局部刷新
- 序言
- Java序列化
- SSH error:a public key file has not been specified by this session
- ServiceLoader跟DriverManager使用总结
- NSURLConnection上传,进度条
- LaTeX修改字体颜色
- java/System/BigInteger/BigDecimal/Date/Calender
- Link Cut Tree学习小记
- CSP考试 2015年09月第3题 模板生成系统 C++实现
- android自定义加载旋转框
- 函数
- ffmpeg在vs2013下编译生成
- opencv学习(6)——图像金字塔