PPNetworkHelper
PPNetworkHelper copied to clipboard
关于多张图片上传的name修复问题(紧急)!!!
- (NSURLSessionTask *)uploadImagesWithURL:(NSString *)URL parameters:(id)parameters name:(NSString *)name images:(NSArray<UIImage *> *)images fileNames:(NSArray<NSString *> *)fileNames imageScale:(CGFloat)imageScale imageType:(NSString *)imageType progress:(PPHttpProgress)progress success:(PPHttpRequestSuccess)success failure:(PPHttpRequestFailed)failure ; 此方法中的name会传给下边方法,
- (void)appendPartWithFileData:(NSData *)data name:(NSString *)name fileName:(NSString *)fileName mimeType:(NSString *)mimeType 我们后台是PHP,同事反馈说多张图片name不能相同,不然只能接收到一张图片(图片会覆盖),建议将name:(NSString *)name改为建议将name:(NSArray *)name;还有问题是你通过一个name可以上传多张图片的话,能告诉我后台接收思路吗?我可以反馈给接口同事,共同进步. -------------------------------------------------------------------------------用代码改变世界.
但是name必须写死为"file", 是指服务器接收的数据类型. 请问有其他解决办法吗
同样遇到这个问题啊
`#pragma mark - 上传多张图片 多个参数
-
(NSURLSessionTask *)uploadImagesWithURL:(NSString *)URL parameters:(id)parameters names:(NSArray *)names images:(NSArray<UIImage *> *)images fileNames:(NSArray<NSString *> *)fileNames imageScale:(CGFloat)imageScale imageType:(NSString *)imageType progress:(PPHttpProgress)progress success:(PPHttpRequestSuccess)success failure:(PPHttpRequestFailed)failure { NSURLSessionTask *sessionTask = [_sessionManager POST:URL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
for (NSUInteger i = 0; i < images.count; i++) { // 图片经过等比压缩后得到的二进制文件 NSData *imageData = UIImageJPEGRepresentation(images[i], imageScale ?: 1.f); // 默认图片的文件名, 若fileNames为nil就使用 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyyMMddHHmmss"; NSString *str = [formatter stringFromDate:[NSDate date]]; NSString *imageFileName = NSStringFormat(@"%@%ld.%@",str,i,imageType?:@"jpg"); [formData appendPartWithFileData:imageData name:[names objectAtIndex:i] fileName:fileNames ? NSStringFormat(@"%@.%@",fileNames[i],imageType?:@"jpg") : imageFileName mimeType:NSStringFormat(@"image/%@",imageType ?: @"jpg")]; }
} progress:^(NSProgress * _Nonnull uploadProgress) { //上传进度 dispatch_sync(dispatch_get_main_queue(), ^{ progress ? progress(uploadProgress) : nil; }); } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (_isOpenLog) {PPLog(@"responseObject = %@",responseObject);} [[self allSessionTask] removeObject:task]; success ? success(responseObject) : nil;
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (_isOpenLog) {PPLog(@"error = %@",error);} [[self allSessionTask] removeObject:task]; failure ? failure(error) : nil;
}];
// 添加sessionTask到数组 sessionTask ? [[self allSessionTask] addObject:sessionTask] : nil ;
return sessionTask; }` 完美解决