AFHTTPRequestOperationManager注意点
AFHTTPRequestOperationManager注意点
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"图片上传" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"相册",nil];
[sheet showInView:self.view];
}
#pragma mark - 实现UIActionSheetDelegate代理方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.delegate = self;
switch (buttonIndex) {
case 0:
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) return;
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
break;
case 1:
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
break;
default:
break;
}
[self presentViewController:ipc animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// 关闭UIImagePickerController控制器
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(@"%@",info);
// 获取图片
UIImage *image = info[UIImagePickerControllerOriginalImage];
self.imageV.image = image;
}
{
// 创建一个管理者
AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
// 设置参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"username"] = @"123";
params[@"pwd"] = @"123";
// 设置URL
NSString *url = @"http://192.168.15.56:8080/MJServer/upload";
[manger POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
需要在这个block中添加文件参数到formData中
NSData *fileData = UIImageJPEGRepresentation(self.imageV.image, 1.0);
[formData appendPartWithFileData:fileData name:@"file" fileName:@"image.png" mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"上传成功");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"上传失败");
}];
}
name : 服务器那边接收文件用的参数名
fileName : (告诉服务器)所上传文件的文件名
mimeType : 所上传文件的文件类型
[formData appendPartWithFileData:fileData name:@"file" fileName:@"image.png" mimeType:@"image/png"];
name : 服务器那边接收文件用的参数名
fileName : (告诉服务器)所上传文件的文件名
mimeType : 所上传文件的文件类型
NSURL *url = [[NSBundle mainBundle] URLForResource:@"itcast" withExtension:@"txt"];
[formData appendPartWithFileURL:url name:@"file" fileName:@"test.txt" mimeType:@"text/plain" error:nil];
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。