码迷,mamicode.com
首页 > Web开发 > 详细

上传视频或者图片

时间:2015-12-24 10:37:12      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

//管理类
@property (nonatomic, retain)AFHTTPRequestOperationManager *manager;
 
//这里用于监听上传任务,多次请求AFN将其加入线程队列
@property(nonatomic, strong) AFHTTPRequestOperation *operation;
 
#pragma mark - 上传视频
-(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];
    }
 
    _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)
        {
            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"];
 
        }
       
    }success:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        NSString *rst = [NSString stringWithFormat:@"%@",[responseObject objectForKey:Rst]];
 
        if ([rst isEqualToString:@"0"])
        {
            NSLog(@"添加视频成功");
        }
        else
        {   
            NSLog(@"添加视频失败");
        }
       
    } failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {  
        NSLog(@"网络请求失败,原因是%@",error);
    }];
}
 
//追踪上传进度
[_operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite)
{
         NSLog(@"上传进度 %.2f%%", (CGFloat)totalBytesWritten/(CGFloat)totalBytesExpectedToWrite*100);
}];
 
个人理解,不到之处请指点

上传视频或者图片

标签:

原文地址:http://www.cnblogs.com/wlsxmhz/p/5072224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!