/**
* 封装POST图片上传(多张图片) // 可扩展成多个别的数据上传如:mp3等
*
* @parammodelurl 请求的链接
* @paramdicData 请求的参数
* @paramimages 存放图片数组
* @paramsuccess 发送成功的回调
* @paramfailure 发送失败的回调
*/
- (void)multiPartPost:(NSDictionary*)dicDataandImageNames:(NSArray*)imagesandImageNamesKey:(NSString*)imageNameURL:(nonnullNSString*)modelurlsuccess:(void(^)(idjson))successfailure:(void(^)(NSError*error))failure {
modelurl = [NSStringstringWithFormat:@"%@%@",projectBaseURL, modelurl];
NSURL*url = [NSURLURLWithString:modelurl];
NSMutableData * bodyData = [NSMutableData data];
for(NSString*keyindicData.allKeys) {
idvalue = [dicDataobjectForKey:key];
[bodyDataappendData:[[NSString stringWithFormat:@"--%@\r\n",@"POST_BOUNDS"] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyDataappendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyDataappendData:[[NSString stringWithFormat:@"%@\r\n",value] dataUsingEncoding:NSUTF8StringEncoding]];
}
for(NSIntegeri =0; i < images.count; i++) {
UIImage* keyN = images[i];
NSString*suffix,*contentType;
NSData* imageData;
if ([FDefultTool calulateImageFileSize:keyN]<10240) {
if(UIImageJPEGRepresentation(keyN,1.0)) {
//返回为JPEG图像。
imageData =UIImageJPEGRepresentation(keyN,1.0);
suffix =@"jpg";
contentType =@"image/jpeg";
}else{
//返回为png图像。
imageData =UIImagePNGRepresentation(keyN);
suffix =@"png";
contentType =@"image/png";
}
}else{
if(UIImageJPEGRepresentation(keyN,1.0)) {
//返回为JPEG图像。
imageData =UIImageJPEGRepresentation(keyN,0.6);
suffix =@"jpg";
contentType =@"image/jpeg";
}else{
//返回为png图像。
imageData =UIImagePNGRepresentation(keyN);
suffix =@"png";
contentType =@"image/png";
}
}
NSString *fileName=@"file";
[bodyDataappendData:[[NSStringstringWithFormat:@"--%@\r\n",@"POST_BOUNDS"]dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSStringstringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@%ld.jpeg\"\r\n",fileName,imageName,(long)i] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyDataappendData:[[NSStringstringWithFormat:@"Content-Type: %@\r\n",contentType]dataUsingEncoding:NSUTF8StringEncoding]];
[bodyDataappendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyDataappendData:imageData];
[bodyDataappendData:[[NSStringstringWithFormat:@"\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];
}
// 添加尾部分隔线
[bodyData appendData:[[NSString stringWithFormat:@"--%@--\r\n",@"POST_BOUNDS"] dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest*request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
[request addValue:[NSString stringWithFormat:@"multipart/form-data;boundary=%@",@"POST_BOUNDS"] forHTTPHeaderField:@"Content-Type"];
[request addValue: [NSString stringWithFormat:@"%zd",bodyData.length] forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[requestsetHTTPBody:bodyData];
[requestsetValue:[NSString stringWithFormat:@"%@",[AcountItemgetToken]]forHTTPHeaderField:@"Authorization"];
AFURLSessionManager*manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[manager dataTaskWithRequest:request uploadProgress:^(NSProgress *_NonnulluploadProgress) {
} downloadProgress:^(NSProgress *_NonnulldownloadProgress) {
} completionHandler:^(NSURLResponse *_Nonnullresponse,id_NullableresponseObject, NSError *_Nullableerror) {
FLog(@"%@", responseObject);
if(!error) {
if(![selfjudgeResultAndhandleWithCode:responseObject]) {
if([responseObject valueForKey:@"messages"]) {
NSError *error= [[NSErroralloc] initWithDomain:@""code:[[responseObjectvalueForKey:@"code"] integerValue] userInfo:@{@"messages":[responseObject valueForKey:@"messages"]}];
failure(error);
return;
}
}
if(success)
{
success(responseObject);
}
// 关闭状态栏动画
[[UIApplicationsharedApplication]setNetworkActivityIndicatorVisible:NO];
}else{
if(error) {
if ([error.userInfo[@"NSLocalizedDescription"] isEqualToString:@"Request failed: unauthorized (401)"]) {
//跳转登录
//清除本地缓存
[[BGDB shareManager]clearTable:APPChatTabName complete:^(BOOLisSuccess) {
[AcountItem removesid];
[SVProgressHUD showErrorWithStatus:@"登录失效,请重新登录"];
AppDelegate *application = (AppDelegate *)[UIApplication sharedApplication].delegate;
[application pushToLoginVC];
}];
}else{
[SVProgressHUD showErrorWithStatus:@"访问失败,请重试"];
}
failure(error);
}
// 关闭状态栏动画
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
}] resume];
}
调用
NSArray *ImageArr=@[IDLeftImage,IDRightImage,DriverLeftImage,DriverRightImage];
[httpTool multiPartPost:@{} andImageNames:ImageArr andImageNamesKey:[FDefultTool GetNowTimes] URL:[NSString stringWithFormat:@"%@?folderType=%d",UpdateImageURL,1] success:^(id _Nonnull json) {
if([json[@"code"]integerValue]==HttpCode) {
[SVProgressHUD showSuccessWithStatus:@"提交成功"];
}
}failure:^(NSError*_Nonnullerror) {
}];
网友评论