Uploading image with AFNetworking 2.0(使用 AFNetworking 2.0 上传图像)
问题描述
我正用这个把头撞到墙上.我想从库中选择 UIImage
并将其上传到服务器,就像使用 <form action="http://blabla.request.cfm" method="post" enctype="multipart/form-data">
.我得到了这个错误,而不是成功:
I'm banging my head against the wall with this one. I want to select UIImage
from library and upload it to server, like on could do with <form action="http://blabla.request.cfm" method="post" enctype="multipart/form-data">
. Instead of success I got this error:
error = Error Domain=NSCocoaErrorDomain Code=3840操作无法完成.(Cocoa 错误 3840.)"(JSON 文本没有以数组或对象开头,并且允许未设置片段的选项.) UserInfo=0x145e5d90{NSDebugDescription=JSON 文本不以数组或对象开头,并允许未设置片段的选项.}
error = Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x145e5d90 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
我试过这样:
-(void)uploadPhoto{
NSString *path = @"http://blabla.request.cfm";
NSData *imageData = UIImageJPEGRepresentation(self.imageView.image, 0.9);
int priv = self.isPrivate ? 1 : 0;
NSDictionary *parameters = @{@"username": self.username, @"password" : self.password, @"private" : @(priv), @"photo" : imageData};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:path parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if(self.imageView.image){
[formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"[UploadVC] success = %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"[UploadVC] error = %@", error);
}];
[self blockView:self.view block:YES];
}
但它不工作...服务器说没有文件.不确定加密是错误的、mime 类型还是什么?
but it's not working... server says that there is no file. Not sure if encrypting is wrong, mime type or what?
也试过这个:
[manager POST:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"[UploadVC] success = %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"[UploadVC] error = %@", error);
}];
还有这个:
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:path parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFormData:imageData name:@"photo"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"[UploadVC] success = %@", responseObject);
[self blockView:self.view block:NO];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"[UploadVC] error response.object = %@", operation.responseObject);
[self blockView:self.view block:NO];
}];
没有任何工作.希望有人可以提供帮助,因为我一直坚持下去,并在 SO
上从一个问题到另一个问题tia
nothing is working. Hope someone can help, 'cause I'm stuck with it and circling from question to question here on SO
tia
编辑:新尝试
1) 首先是多部分形式
2) 创建上传任务
他们都没有为我工作,所以我仍在努力解决这个问题,但看不到任何解决方案
EDIT: new attempt
1) first was multi-part form
2) creating upload task
none of them worked for me, so I'm still trying to cope with that, but cannot see any solution
推荐答案
我不确定是哪部分(我认为缺少一些细节)负责,但我终于做到了:) 给你:
I'm not sure which part (I think that some details were missing) was responsible, but I did it finally :) here you go:
-(void)uploadPhoto{
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://server.url"]];
NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5);
NSDictionary *parameters = @{@"username": self.username, @"password" : self.password};
AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//do not put image inside parameters dictionary as I did, but append it!
[formData appendPartWithFileData:imageData name:paramNameForImage fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
[op start];
}
像魅力一样工作:)
这篇关于使用 AFNetworking 2.0 上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 AFNetworking 2.0 上传图像
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01