iOS 多文件上传

2020-11-30  本文已影响0人  可乐小子

// if ([formStyle isEqualToString:@"video"]) {
// [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"mp4"];
// }else {
// [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/png"];
// }

            for (int i=0; i<fileDatas.count; i++) {
                       NSDictionary* unitData = fileDatas[i];
                 
                              NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                              // 设置时间格式
                              formatter.dateFormat = @"yyyyMMddHHmmss";
                              NSString *str = [formatter stringFromDate:[NSDate date]];
                
                       if (unitData) {
                           if ([[unitData objectForKey:@"image"] isKindOfClass:[NSString class]]) {

// [formData appendPartWithFileData:(NSData*)[unitData objectForKey:@"image"] name:@"image" fileName:@"image" mimeType:@"multipart/form-data"];
NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
[formData appendPartWithFileData:[[unitData objectForKey:@"image"] dataUsingEncoding:NSUTF8StringEncoding] name:@"file" fileName:fileName mimeType:@"image/png"];

                           }else if([[unitData objectForKey:@"video"] isKindOfClass:[NSString class]]){
                               //if([[unitData objectForKey:@"video"] isKindOfClass:[NSString class]])
           NSString *fileName = [NSString stringWithFormat:@"%@.mp4", str];
                                [formData appendPartWithFileData:[[unitData objectForKey:@"video"] dataUsingEncoding:NSUTF8StringEncoding]  name:@"file" fileName:fileName mimeType:@"mp4"];
                               
           
                           }
                       
                       }
            }
       } progress:^(NSProgress * _Nonnull uploadProgress) {
            
           //上传进度
           // @property int64_t totalUnitCount;     需要下载文件的总大小
           // @property int64_t completedUnitCount; 当前已经下载的大小
           //
           // 给Progress添加监听 KVO
           NSLog(@"上传速度   %f",1.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
           // 回到主队列刷新UI,用户自定义的进度条
           
       } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
           NSLog(@"上传成功 %@", responseObject);
           successBlock(responseObject);
       } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
           NSLog(@"上传失败 %@", error);
       }];

}

上一篇下一篇

猜你喜欢

热点阅读