使用 AFNetworking 发布图像?
Posted
技术标签:
【中文标题】使用 AFNetworking 发布图像?【英文标题】:Post a image using AFNetworking? 【发布时间】:2014-05-09 13:23:04 【问题描述】:如何使用AFNetworking
发布图片。我正在使用下面的代码发布图片。但是图片没有上传到服务器。发布图片后,我在结果“http://thisisswitch.com:8084/SmartSwitchService/UserImages/69684177-4601-4665-8890-5424e3fbff73.png”中获得了这个链接。
谁能告诉我代码有什么问题?
这是我的代码:
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:BaseUrl]];
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:posturl parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
[formData appendPartWithFormData:imgData name:@"image"];
];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
NSDictionary *dict = (NSDictionary *)JSON;
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
];
[operation start];
【问题讨论】:
您确定问题不在您的 php 中吗?如果文件不是在服务器上创建的,则不应返回链接...您使用的是什么后端?自己写的? 我对这一行有疑问 [formData appendPartWithFormData:imgData name:@"image"];这一行的名字是什么 来自后端的.net 您使用的是 AFNetworking 1 还是 2? 【参考方案1】:来自Github read me:
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error)
if (error)
NSLog(@"Error: %@", error);
else
NSLog(@"Success: %@ %@", response, responseObject);
];
[uploadTask resume];
【讨论】:
【参考方案2】:这是我的做法(在 AFNetworking 2.x 中):
- (void)requestWithFormData:(void (^)(id <AFMultipartFormData> formData))formBlock
withParams:(NSDictionary *)params
webservicePath:(NSString *)webservicePath
success:(void (^)(AFHTTPRequestOperation *operation, id jsonDictionary))successBlock
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failureBlock
// Handle request for file upload
AFHTTPRequestOperation *op = [self.manager POST:webservicePath
parameters:params
constructingBodyWithBlock:formBlock
success:success
failure:failureBlock];
[op start];
然后我按照以下方式使用它:
void (^formBlock)(id <AFMultipartFormData> formData) = ^(id <AFMultipartFormData>formData)
NSData *data1 = UIImagePNGRepresentation(profileImage);
[formData appendPartWithFileData:data1 name:@"file" fileName:@"picture.png" mimeType:@"image/png"];
;
[networkingUtil requestWithFormData:formBlock
withParams:params
webservicePath:@"myserver.com/upload"
success:successBlock
failure:failureBlock];
您的服务器应该处理表单数据并查找file
。
【讨论】:
以上是关于使用 AFNetworking 发布图像?的主要内容,如果未能解决你的问题,请参考以下文章