• 上传视频或者图片


    //管理类
    @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);
    }];
     
    个人理解,不到之处请指点
  • 相关阅读:
    chrome输入框记住密码导致背景黄色的解决方案
    死活要居中(转载)
    Google HTML/CSS/JS代码风格指南
    CSS的inherit与auto使用分析
    photoshop中rgb与索引模式的区别
    placeholder调整颜色
    你应该了解的5个JavaScript调试技巧
    HTML TAG FROM MDN
    apple iphone 3gs 有锁机 刷机 越狱 解锁 全教程(报错3194,3014,1600,短信发不出去等问题可参考)
    史上最全的CSS hack方式一览
  • 原文地址:https://www.cnblogs.com/wlsxmhz/p/5072224.html
Copyright © 2020-2023  润新知