美文网首页
iOS 多文件上传

iOS 多文件上传

作者: 可乐小子 | 来源:发表于2020-11-30 16:54 被阅读0次
    • (void)uploadTaskUrl:(NSString )uploadPage params:(NSDictionary)params files:(NSArray<NSDictionary<NSString,id>>)fileDatas success:(void(^)(id object))successBlock error:(void(^)(id object))errorBlock {
      NSLog(@"url %@\nparam %@",uploadPage,params);
      NSError
      error = NULL;
      NSString *urlString = [NSString stringWithFormat:@"%@%@",Chat_APP_SERVER_ADDRESS,uploadPage];

      AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
      NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
      [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];

           [manager POST:urlString parameters:[self  md5CodesignSortDict:params]  constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
                
              
               //上传
                /*
                此方法参数
                    1. 要上传的[二进制数据]
                    2. 对应网站上[upload.php中]处理文件的[字段"file"]
                    3. 要保存在服务器上的[文件名]
                    4. 上传文件的[mimeType]
               */
      

    // 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);
           }];
    

    }

    相关文章

      网友评论

          本文标题:iOS 多文件上传

          本文链接:https://www.haomeiwen.com/subject/gxfdwktx.html