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

IOS 视频上传服务器

时间:2018-12-23 15:19:58      阅读:494      评论:0      收藏:0      [点我收藏+]

标签:contain   defaults   lse   rom   ict   视频   header   hmm   ota   

//上传视频

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 20;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"application/json", @"text/html", @"text/json", nil];
    //上传服务器接口
    [manager.requestSerializer setValue:[USER_DEFAULTS objectForKey:@"token"] forHTTPHeaderField:@"dd"];

    NSData* data=[NSData dataWithContentsOfURL:self.VideoURL];
    
    [self showRoundProgressWithTitle:@"正在上传"];
    
    [manager POST:[NSString stringWithFormat:@"%@a_doutu/push_doutu/",URLPATH] parameters:sendDict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:data name:@"video" fileName:@"video.mp4"mimeType:@"video/mp4"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        NSLog(@"upload:%@",uploadProgress.fileTotalCount);
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
//        [self hideBubble];
        
        if ([[responseObject objectForKey:@"code"] intValue] == 203) {
            [self showRightWithTitle:@"发布成功" autoCloseTime:1];
            [self performSelector:@selector(afterLoad) withObject:nil afterDelay:1.5];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [self showErrorWithTitle:[responseObject objectForKey:@"message"] autoCloseTime:1];
            [self performSelector:@selector(afterLoad) withObject:nil afterDelay:1.5];
        }
        
//            NSString *resMsg = responseObject[@"resMsg"];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hideBubble];
        [self  showErrorWithTitle:@"网络错误" autoCloseTime:1];
    }];

 

//上传图片

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        manager.requestSerializer.timeoutInterval = 10.0f;
        [manager POST:[NSString stringWithFormat:@"%@blog/api/upload_head",MYURL] parameters:send_dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"yyyyMMddHHmmss";
            NSString *str = [formatter stringFromDate:[NSDate date]];
            NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
            //上传图片
            [formData appendPartWithFileData:imageData //要上传的文件
                                        name:@"pic"  //这个参数是后台取文件参数的时候用的名字,如果没有的话就传空字符串
                                    fileName:fileName//文件上传的名字,可以随便取,但是不要重名,所以一般是用拼接当前时间的方式作为文件名
                                    mimeType:@"image/jpeg"];
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            //上传成功
            id jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers  error:nil];
            
            NSLog(@"-=-= %@",jsons);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            //上传失败
            
        }];

IOS 视频上传服务器

标签:contain   defaults   lse   rom   ict   视频   header   hmm   ota   

原文地址:https://www.cnblogs.com/xiyangxixia/p/10164159.html

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