//假设自己的视频文件URL为 变量myVideoURL
//1.获取视频文件名字
NSString *videoName = [myVideoURL lastPathComponent];
//2.将视频URL转位Data
NSData *dataFromURL = [[NSData alloc] initWithContentsOfURL:myVideoURL];
//3.将上传地址转为NSString
NSString *url [NSStringstringWithFormat:@"http://api.myVideo"];
//4.定义字典,将需要给服务器传参,写进字典
NSMutableDictionary *parameters = [[NSMutableDictionary alloc]init];
//5.下面是AFNetWorking的接口函数
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/json",@"application/json",@"text/javascript", nil];
[manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
//以下三个参数依次为:视频,视频名字,视频格式
[formData appendPartWithFileData:dataFromURL name:@"file" fileName:videoName mimeType:@"video/quicktime"];
} success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"成功,结果是:%@",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"失败,原因是:%@",error);
} ];
**************************************************************************************
//如果需要给视频上传增加进度条,请使用下面的方法。
//加载进度条
UIProgressView *progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
progressView.frame = CGRectMake(0, 52, screen_width, 10);
//设置进度条背景颜色
progressView.trackTintColor = [UIColor whiteColor];
//设置进度条进度颜色
progressView.progressTintColor = [UIColor redColor];
//设置进度默认值
progressView.progress = 0.0;
[self.view addSubview:progressView];
AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
NSMutableURLRequest *request =
[serializer multipartFormRequestWithMethod:@"POST" URLString:url
parameters:parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:dataFromURL
name:@"file"
fileName:videoName
mimeType:@"video/quicktime"];
}];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestOperation *operation =
[manager HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure %@", error.description);
}];
[operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
long long totalBytesWritten,
long long totalBytesExpectedToWrite) {
float t1 = totalBytesWritten;
float t2 = totalBytesExpectedToWrite;
float progress = t1/t2;
[progressView setProgress:progress animated:YES];
NSLog(@"比例是:%lf",progress);
}];
[operation start];