NSURLConnection上传,进度条

结合上一篇下载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;
}

  NSURLConnection上传,进度条_第1张图片

你可能感兴趣的:(进度条,NSURLConnection)