AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", nil]];
[manager POST:url parameters:body constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
// 获取图片数据
NSData *fileData = UIImageJPEGRepresentation(imageData, 1.0);
// 设置上传图片的名字
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
[formData appendPartWithFileData:fileData name:@"userfile" fileName:fileName mimeType:@"image/png"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 返回结果
success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(error);
}];
- 参数
- name : 上传文件的key
- mimeType:image/png
- fileName:上传图片时图片的名字根据