iOS开发通过AFNetworking上传图片到服务器
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
@"text/html",
@"image/jpeg",
@"image/png",
@"application/octet-stream",
@"text/json",
nil];
manager.requestSerializer= [AFHTTPRequestSerializer serializer];
manager.responseSerializer= [AFHTTPResponseSerializer serializer];
[manager POST:@"http://www.xingzhou.win/?c=api&a=image_input" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
UIImage *image = [UIImage imageNamed:@"gauge.png"];
NSData *data = UIImagePNGRepresentation(image);
//上传的参数(上传图片,以文件流的格式)
[formData appendPartWithFileData:data
name:@"file"
fileName:@"gauge.png"
mimeType:@"image/png"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"上传成功");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"上传失败%@",error);
}];