AFNetworking将图像作为文件而不是作为数据发送 [英] AFNetworking send image as file not as data
本文介绍了AFNetworking将图像作为文件而不是作为数据发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在发送数据的高服务器要求图像是文件(jpg),而不是NSData。该代码有效,但是服务器无法将NSData识别为图像。有什么想法要解决吗?
Hi server that I'm sending data is expecting image as file(jpg) not as NSData. This code works, but server can't recognize NSData as image. Any thoughts how to solve this?
+ (void)signupWithParameters:(NSDictionary *)parameters
andUserHaveImage:(BOOL)userHaveImage
andImage:(NSData *)image
successBlock:(void (^) (NSDictionary *response))successHandler
errorBlock:(void (^) (NSDictionary *error))errorHandler
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"multipart/formdata" forHTTPHeaderField:@"Content-Type"];
[manager POST:[NSString stringWithFormat:@"%@/api/1.0/customer/sign-up", DEFAULT_URL] parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
if (userHaveImage == YES) {
//
[formData appendPartWithFileData:image name:@"img_profile" fileName:@"profileImage.jpg" mimeType:@"image/jpg"];
[formData appendPartWithFormData:image name:@"img_profile"];
}
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
successHandler(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
errorHandler(@{});
}];
}
推荐答案
通过将图像转换为UTF8字符串并以该格式发送来解决。
Solved by converting image to UTF8 string and sending it in that format.
这篇关于AFNetworking将图像作为文件而不是作为数据发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文