一般我们都会base64上传图片,代码如下:
- (void)uploadPhotoToSrver:(UIImage *) image withPhotoID:(NSString *)photoID andPhotoType:(PhotoType)type andImageText:(NSString *)text {
static int upLoadCount = 0;
NSData *imageData = UIImageJPEGRepresentation(scaledImg, 0.6);
NSString *dataStr = [imageData base64EncodedStringWithOptions:0];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/xml"];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//参数
NSDictionary *parameters = @{@"UserID" : userInfo.UserID, @"imageDataStr": dataStr};
// url String
NSString *baseURL = [NSString stringWithFormat: @"%@%@", WEBBASEURL, WEBUploadPhotoDataAction];
[manager POST:baseURL parameters:parameters success:^(AFHTTPRequestOperation *operation,id responseObject) {
NSLog(@"上传成功");
}failure:^(AFHTTPRequestOperation *operation,NSError *error) {
MYNSLOG(@"~~~~~~~~%@",error);
}];
}
然后服务器要求file文件格式上传:
服务器要求需要传入的参数下图所示:
具体通过AFN实现代码如下所示:
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1)];
ECGFeedbackInfo *info = [[ECGFeedbackInfo alloc]init];
info.imageData = imageData;
info.feedbackOpinion = @"bug-bug-bug";
info.phoneNum = @“1888888888”;
[[ECGpatientService sharedInstance] uploadFeedbckImageWithFeedbackInfo:info alertContent:alertContent success:^(id obj) {
NSLog(@"用户反馈意见上传成功");
successBlcok();
} fail:^{
NSLog(@"用户反馈意见上传失败");
}];
- (void)uploadFeedbckImageWithFeedbackInfo:(ECGFeedbackInfo *)info alertContent:(NSString *)content success:(SuccessBlockWithObject)success fail:(void (^)())fail {
NSString *url = @“你的URL地址”;
NSDictionary *parameter = [NSDictionary
dictionaryWithObjectsAndKeys:info.phoneNum,@"phone",info.feedbackOpinion,nil];
[ECGAFNetWorking uploadFeedbackImageWithURLString:url content:content parameters:parameter uploadDatas:info.imageData completeSuccess:^(NSDictionary *respinseDic, id responseObject) {
EcgResponseMessage *responseMessage = [[EcgResponseMessage alloc] initWithPropertyDictionary:respinseDic];
success(responseMessage);
} completeFailure:^{
fail();
}];
}
/**
上传图片
@param URLString URL
@param content 弹框的内容
@param parameters 参数体
@param uploadDatas 上传图片NSData
@param completeSuccess 成功回调
@param completeFailure 失败回调
*/
+ (void)uploadFeedbackImageWithURLString:(NSString *)URLString content:(NSString *)content parameters:(id)parameters uploadDatas:(NSData *)uploadImageData completeSuccess:(void (^)(NSDictionary *respinseDic, id responseObject))completeSuccess completeFailure:(void (^)())completeFailure{
NSString *tips = nil;
// 展示等待框
if ([Public isNum:content]) {
tips = LOAD_STRING(@"net_wait");
}else {
tips = content;
}
// 弹框
[ECGCustomAlertView showWaitPopViewWithContent:tips];
URLString = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPSessionManager *manager = [AFHTTPSessionManager
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/html",nil];
// 设置超时时间
[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
manager.requestSerializer.timeoutInterval = 20.f;
[manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
__weak typeof(self) weakSelf = self;
[manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id< AFMultipartFormData > _Nonnull formData) {
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
formatter.dateFormat=@"yyyyMMddHHmmss";
NSString *str=[formatter stringFromDate:[NSDate date]];
NSString *fileName=[NSString stringWithFormat:@"%@.jpg",str];
[formData appendPartWithFileData:uploadImageData name:@"content_pic" fileName:fileName mimeType:ECGBJKeyJPEG];
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 转换responseObject对象
NSDictionary *dict = nil;
if ([responseObject isKindOfClass:[NSDictionary class]]) {
dict = (NSDictionary *)responseObject;
} else {
dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
}
// 成功后弹框处理,回调出去
[weakSelf notDismissedWith:dict responseObject:responseObject complete:^(NSDictionary *respinseDic, id responseObject) {
completeSuccess(respinseDic,responseObject);
} failedComplete:^{
completeFailure();
}];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// 网络问题,弹框处理
[weakSelf notNetconnetNotDismissComplete:^{
completeFailure();
}];
}];
}