一、上传多张图片
备注:支持上传不同类型的图片(举例:imageDatas
图片数组最后一项image
元素对应不同参数名)
// 上传图片
- (void)postDataWithUrl:(NSString *)url params:(NSMutableDictionary *)params imageDatas:(NSArray *)images success:(void (^)(id response))success failure:(void (^)(NSError *error))failure
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json",@"text/html",@"text/javascript",@"text/xml",@"text/plain",nil]];
[manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
for (UIImage *image in images) {
// 图片太大会上传不到服务器上面去
NSData *imageData = UIImageJPEGRepresentation(image, 0.1);;
// 在网络开发中,上传文件时,是文件不允许被覆盖,文件重名
// 要解决此问题,
// 可以在上传时使用当前的系统事件作为文件名
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// 设置时间格式
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
/*
此方法参数
1. 要上传的[二进制数据]
2. 对应网站上[upload.php中]处理文件的[字段"file"]
3. 要保存在服务器上的[文件名]
4. 上传文件的[mimeType]
*/
NSUInteger index = [images indexOfObject:image];
if (index == images.count-1) {
// 上传位置图片data
[formData appendPartWithFileData:imageData name:@"posfile" fileName:fileName mimeType:@"image/png"];
} else {
[formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/png"];
}
}
} progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"uploadProgress = %@",uploadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(error);
}];
}
二、上传语音文件
备注:支持上传图片、语音同时上传;根据自己的需求定制化调整代码即可
// 上传语音
- (void)POSTOneAmrWithUrl:(NSString *)url params:(NSMutableDictionary *)params voiceName:(NSString *)fileName AmrDatas:(NSData *)amr imageDatas:(NSArray *)images success:(void (^)(id response))success failure:(void (^)(NSError *error))failure
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json",@"text/html",@"text/javascript",@"text/xml",@"text/plain",nil]];
[manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:amr name:@"voicefile" fileName: [NSString stringWithFormat:@"%@.mp3", fileName] mimeType:@"amr/mp3/wmr"];
for (UIImage *image in images) {
// 图片太大会上传不到服务器上面去
NSData *imageData = UIImageJPEGRepresentation(image, 0.1);;
// 在网络开发中,上传文件时,是文件不允许被覆盖,文件重名
// 要解决此问题,
// 可以在上传时使用当前的系统事件作为文件名
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// 设置时间格式
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
/*
此方法参数
1. 要上传的[二进制数据]
2. 对应网站上[upload.php中]处理文件的[字段"file"]
3. 要保存在服务器上的[文件名]
4. 上传文件的[mimeType]
*/
NSUInteger index = [images indexOfObject:image];
if (index == images.count-1) {
// 上传位置图片data
[formData appendPartWithFileData:imageData name:@"posfile" fileName:fileName mimeType:@"image/png"];
} else {
[formData appendPartWithFileData:imageData name:@"file" 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);
}];
}