// // UploadImgVo.m // // // Created by cs on 15/8/24. // Copyright (c) 2015年. All rights reserved. // #import "UploadImgVo.h" #import "AFNetSessionClient.h" #import "AppDelegate.h" #import "LoginMainViewController.h" @implementation UploadImgVo +(NSURLSessionDataTask *)uploadImg:(NSString *)strPath withDic:(NSDictionary *)parameters withSuccess:(AFNetSessionSuccessModelBlock)success withFialure:(AFNetSessionFailBlock)failure{ [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES]; AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; NSString *strToken; NSString *strUserId; if (appDelegate.loginMainViewController.isLogin) { strToken = appDelegate.loginMainViewController.user.token; strUserId = [NSString stringWithFormat:@"%d",appDelegate.loginMainViewController.user.mid]; }else{ strToken = DefaultToken; strUserId = @"0"; } [parameters setValue:strToken forKey:@"token"];//artist/uploadImg return [[AFNetSessionClient sharedClient] POST:[NSString stringWithFormat:@"user/%@/%@",strUserId,strPath] parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO]; UIImage *image = [parameters valueForKey:@"image"]; NSData *imageData = UIImageJPEGRepresentation(image, 0.5); // NSData *imageData = UIImagePNGRepresentation(image); NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyyMMddHHmmss"; NSString *str = [formatter stringFromDate:[NSDate date]]; NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str]; // NSString *fileName = [NSString stringWithFormat:@"%@.png", str]; [formData appendPartWithFileData:imageData name:@"imgFile" fileName:fileName mimeType:@"image/jpg"]; } success:^(NSURLSessionDataTask *task, id responseObject) { DLOG(@"图片上传成功"); NSError *error; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error]; if (success) { if (!error.code == 0) { failure(error,nil,nil); }else{ NSString *status =[dic valueForKey:@"status"]; if ([status isEqualToString:@"200" ]) { success(dic); } else { NSString *message = [dic valueForKey:@"message"]; failure(nil,status,message); } } } } failure:^(NSURLSessionDataTask *task, NSError *error) { DLOG(@"上传图片异常::%@",error.description); [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO]; if (failure) { failure(error,nil,nil); } }]; } +(NSURLSessionDataTask *)uploadMp3:(NSString *)strPath withDic:(NSDictionary *)parameters withSuccess:(AFNetSessionSuccessModelBlock)success withFialure:(AFNetSessionFailBlock)failure{ [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES]; AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; NSString *strToken; NSString *strUserId; if (appDelegate.loginMainViewController.isLogin) { strToken = appDelegate.loginMainViewController.user.token; strUserId = [NSString stringWithFormat:@"%d",appDelegate.loginMainViewController.user.mid]; }else{ strToken = DefaultToken; strUserId = @"0"; } [parameters setValue:strToken forKey:@"token"]; return [[AFNetSessionClient sharedClient] POST:[NSString stringWithFormat:@"user/%@/%@",strUserId,strPath] parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO]; NSString *mp3Path = [parameters valueForKey:@"mp3"]; NSData* mp3Data= [NSData dataWithContentsOfFile:mp3Path]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyyMMddHHmmss"; NSString *str = [formatter stringFromDate:[NSDate date]]; NSString *fileName = [str stringByAppendingPathExtension:@"mp3"]; [formData appendPartWithFileData:mp3Data name:@"imgFile" fileName:fileName mimeType:@"audio/mp3"]; } success:^(NSURLSessionDataTask *task, id responseObject) { DLOG(@"文件上传成功"); NSError *error; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error]; if (success) { if (!error.code == 0) { failure(error,nil,nil); }else{ NSString *status =[dic valueForKey:@"status"]; if ([status isEqualToString:@"200" ]) { success(dic); } else { NSString *message = [dic valueForKey:@"message"]; failure(nil,status,message); } } } } failure:^(NSURLSessionDataTask *task, NSError *error) { DLOG(@"上传图片异常::%@",error.description); [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO]; if (failure) { failure(error,nil,nil); } }]; } @end
上传图片和mp3
猜你喜欢
转载自wenxin2009.iteye.com/blog/2337275
今日推荐
周排行