使用AFN上传视频

3年前有写过上传图片的文章,今天在这记录下上传视频的方法!上传图片和视频都属于文件上传(upload file),其实是类似的,只需要在FormData时放入不同文件的二进制,再填写上传文件的mimeType(视频(video/mp4),图片(image/jpeg))即可。我在这里使用AFHTTPSessionManager 写下上传方法:

.h
  /***
  * brief 上传视频
  * params view : 显示进度的视图
  * finish 返回上传后的视频地址信息
  ***/
 + (void)uploadVideoWithFilePath:(NSString *)filePath view:(UIView *)view progress:(UploadProgress)progress andFinish:(void (^)(NSDictionary *result, NSError *error))finish;

.m
//上传视频 实现
+ (void)uploadVideoWithFilePath:(NSString *)filePath view:(UIView *)view progress:(UploadProgress)progress andFinish:(void (^)(NSDictionary *, NSError *))finish {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 15;
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    NSString *x_auth_token = @"校验权限的x-auth-token";
    [manager.requestSerializer setValue:x_auth_token forHTTPHeaderField:@"x-auth-token"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain",@"text/json", @"text/javascript", @"text/html",nil];
   NSString *url = @"上传服务器的地址";
    [manager POST:url parameters:params   constructingBodyWithBlock:^(id  _Nonnull formData) {
        //上传视频的参数
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.mp4", str];
        NSData *fileData = [NSData dataWithContentsOfFile:filePath];
        [formData appendPartWithFileData:fileData name:@"file" fileName:fileName mimeType:@"video/mp4"];
  } progress:^(NSProgress * _Nonnull uploadProgress) {
        dispatch_async(dispatch_get_main_queue(), ^{
            //进度在主线程中刷新UI界面
            NSLog(@"uploadProgress.completedUnitCount-->%lld", uploadProgress.completedUnitCount);
            NSLog(@"uploadProgress.totalUnitCount-->%lld", uploadProgress.totalUnitCount);
            NSNumber *progressValue = @(1.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
            NSNumber *progressPercent = @(100.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
            NSString *progressToast = [NSString stringWithFormat:@"%zd%%", [progressPercent integerValue]];
            progress(progressValue, progressToast);
        });
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"responseObject-->%@",responseObject);
        if (responseObject[@"code"].intValue == 200) { //成功
              finish(responseObject, nil);
        } else { //失败
              NSError *error = [[NSError alloc] init];
              finish(nil,error);
        }
   } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { //网络问题
          //网络异常
          finish(nil, error);
   }];
}

你可能感兴趣的:(使用AFN上传视频)