1. 程式人生 > >IOS 視訊上傳伺服器

IOS 視訊上傳伺服器

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];
    }];