iOS ASI--文件上传
1.文件上传的示例代码
// 设置url
NSURL *url = [NSURL URLWithString:@"http://localhost/photo"];
// 设置请求
self.postRequest = [ASIFormDataRequest requestWithURL:url];
// 指定要上传文件的路径
NSString *file = [[NSBundle mainBundle] pathForResource:@"123.txt" ofType:nil];
// 设置和文件相关的参数
[self.postRequest setFile:file forKey:@"file"]; // 方法1,客户端本地文件的文件名是是什么,传到服务器端的文件名就是什么
// [self.postRequest setFile:file withFileName:@"newName.txt" andContentType:@"text/plain" forKey:@"file"]; // 方法2,可以自定义文件名
// NSData *data = [NSData dataWithContentsOfFile:file]; // 方法3,在无法获取本地文件的path时,适合用这个方法,比如拍照上传
// [self.postRequest setData:data forKey:@"file"];
// 设置其他请求参数
[self.postRequest setPostValue:@"ios" forKey:@"username"];
[self.postRequest setPostValue:@"123" forKey:@"pwd"];
// 接收到服务器返回的数据
[self.postRequest setCompletionBlock:^{
NSLog(@"上传完咯");
}];
// 发起请求
[self.postRequest startAsynchronous];
2.照片上传
2.1先把照片写入相册. 写入前,需要用户授权,iOS会自动处理授权;照片写入相册后,可以在模拟器的相册里看到照片
UIImage *image = [UIImage imageNamed:@"123.png"];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
2.2 获取照片
// 打开相册
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self pickPhoto];
}
- (void)pickPhoto{
UIImagePickerController *pick = [[UIImagePickerController alloc]init];
pick.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //设置照片来源,第1个和第3个差不多,都是来源相册;第2个是来源相机
// UIImagePickerControllerSourceTypePhotoLibrary,
// UIImagePickerControllerSourceTypeCamera,
// UIImagePickerControllerSourceTypeSavedPhotosAlbum
pick.delegate = self; //要做照片选择器的代理,需要遵守2个协议UINavigationControllerDelegate, UIImagePickerControllerDelegate
[self presentViewController:pick animated:YES completion:nil];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *image = info[UIImagePickerControllerOriginalImage];
[self upload:image];
}
- (void)upload:(UIImage *)image{
// 设置url
NSURL *url = [NSURL URLWithString:@"http://localhost/photo"];
// 设置请求
self.postRequest = [ASIFormDataRequest requestWithURL:url];
NSData *data = UIImagePNGRepresentation(image);
[self.postRequest setData:data withFileName:@"newImage.png" andContentType:@"image/png" forKey:@"file"];
// 设置其他请求参数
[self.postRequest setPostValue:@"ios" forKey:@"username"];
[self.postRequest setPostValue:@"123" forKey:@"pwd"];
// 接收到服务器返回的数据
[self.postRequest setCompletionBlock:^{
NSLog(@"上传完咯");
}];
// 发起请求
[self.postRequest startAsynchronous];
}
注:上面的代码是上传相册里的照片,如果是上传相机刚刚拍好的照片,只需要把 pick.sourceType = UIImagePickerControllerSourceTypePhotoLibrary, 改成pick.sourceType = UIImagePickerControllerSourceTypeCamera,其他代码不用改.
3.大文件上传,要注意大文件上传不要用data上传,应该用file上传,因为把大文件序列化成NSData很耗性能,也没那么大的内存
// 设置url
NSURL *url = [NSURL URLWithString:@"http://localhost/bigDataFolder"];
// 设置请求
self.postRequest = [ASIFormDataRequest requestWithURL:url];
// 假设大文件在cache里
NSString *cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [cache stringByAppendingPathComponent:@"data.zip"];
[self.postRequest setFile:filePath forKey:@"file"];
// 设置其他请求参数
[self.postRequest setPostValue:@"ios" forKey:@"username"];
[self.postRequest setPostValue:@"123" forKey:@"pwd"];
// 上传进度
self.postRequest.uploadProgressDelegate = self.progressView;
// 监听请求
[self.postRequest setCompletionBlock:^{
NSLog(@"上传完咯");
}];
// 发起请求
[self.postRequest startAsynchronous];
补充:ASI不支持断点上传,ASI是基于HTTP协议,而HTTP协议不支持断点上传,TCP/IP支持