AFNetworking send image as file not as data

蓝咒 提交于 2019-12-08 05:25:09

问题


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/form­data" 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(@{});
}];

}


回答1:


Solved by converting image to UTF8 string and sending it in that format.



来源:https://stackoverflow.com/questions/40058781/afnetworking-send-image-as-file-not-as-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!