上传视频或者图片
//管理类
@property (nonatomic, retain)AFHTTPRequestOperationManager *manager;
//这里用于监听上传任务,多次请求AFN将其加入线程队列
@property(nonatomic, strong) AFHTTPRequestOperation *operation;
#pragma mark - 上传视频
-(void)postToUploadVideo
-(void)postToUploadVideo
{
_manager = [AFHTTPRequestOperationManager manager];
_manager.responseSerializer = [AFJSONResponseSerializer serializer];
_manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
//网络请求需要传递的参数
NSMutableDictionary *parameters=[[NSMutableDictionary alloc] init];
if ([super JudgeString:App_Value])
{
[parameters setObject:App_Value forKey:App];
if ([super JudgeString:App_Value])
{
[parameters setObject:App_Value forKey:App];
}
_operation = [[AFHTTPRequestOperation alloc]init];
//YBN_AddVideo_URL : 后台给的请求网址
//parameters:parameters : 网络请求的参数
_operation = [_manager POST:YBN_AddVideo_URL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
if (self.videoURL != nil)
{
{
if (self.videoURL != nil)
{
NSError *error;
//移除之前添加的“file://”
NSString *filePath = [[self.videoURL absoluteString] stringByReplacingOccurrencesOfString:@"file://" withString:@""];
//添加上传视频
// name:@"shipin” : 网络请求的参数数名
//fileName:[NSString stringWithFormat:@"%@.mp4”
:上传服务器后视频的名称
//mimeType: : 上传视频的格式
[formData appendPartWithFileURL:[NSURL fileURLWithPath:filePath] name:@"shipin" fileName:[NSString stringWithFormat:@"%@.mp4",[CYLFolderConfig_lib timeStamp]] mimeType:@"video/mp4" error:&error];
if (error)
{
NSLog(@"上传视频错误原因:%@",error);
}
}
if (self.bgImage.image != nil)
{
//添加上传图片
[formData appendPartWithFileData:[self narrowImageWithImage:self.bgImage.image] name:@"fengmian" fileName:[NSString stringWithFormat:@"%@.jpeg",[CYLFolderConfig_lib timeStamp]] mimeType:@"image/jpeg"];
if (error)
{
NSLog(@"上传视频错误原因:%@",error);
}
}
if (self.bgImage.image != nil)
{
//添加上传图片
[formData appendPartWithFileData:[self narrowImageWithImage:self.bgImage.image] name:@"fengmian" fileName:[NSString stringWithFormat:@"%@.jpeg",[CYLFolderConfig_lib timeStamp]] mimeType:@"image/jpeg"];
}
}success:^(AFHTTPRequestOperation *operation, id responseObject)
{
}success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSString *rst = [NSString stringWithFormat:@"%@",[responseObject objectForKey:Rst]];
if ([rst isEqualToString:@"0"])
{
{
NSLog(@"添加视频成功");
}
else
else
{
NSLog(@"添加视频失败");
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"网络请求失败,原因是%@",error);
}];
}
//追踪上传进度
[_operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite)
{
NSLog(@"上传进度 %.2f%%", (CGFloat)totalBytesWritten/(CGFloat)totalBytesExpectedToWrite*100);
}];
个人理解,不到之处请指点