IOS 视频.图片上传服务器

时间:2020-12-16 04:39:19

//上传视频

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"  //这个参数是后台取文件参数的时候用的名字,如果没有的话就传空字符串      如果后台要求请求参数为数组则为@"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) {
            //上传失败
            
        }];