AFN上传视频

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
    
    NSString *urlString =UpLoadUrl;

    //    //post请求
    
    [manager POST:urlString parameters:nil constructingBodyWithBlock:^(id  _Nonnull formData) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat   = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.mp4", str];
        /*
         此方法参数
         1. 要上传的[二进制数据]
         2. 我这里的imgFile是对应后台给你url里面的图片参数,别瞎带。
         3. 要保存在服务器上的[文件名]
         4. 上传文件的[mimeType]
         */
        NSURL * videoUrl = [NSURL URLWithString:path];
//        NSLog(@"视频地址是====%@",videoUrl);
        [formData appendPartWithFileURL:videoUrl name:@"mfile" fileName:fileName mimeType:@"video/mpeg4" error:(nil)];

    } progress:^(NSProgress * _Nonnull uploadProgress) {
        float progress =  1.0 * uploadProgress.completedUnitCount/uploadProgress.totalUnitCount;
//        NSLog(@"上传视频进度%f",progress);
        //找到message大数组中对应数据以更改,加入上传进度
        NSString *who;
        int which = 0;
        NSString *file1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"message.count"];
        if (![[NSFileManager defaultManager] fileExistsAtPath:file1]) {
        }else{
            
            NSMutableDictionary *countDic = [[NSMutableDictionary alloc]initWithContentsOfFile:file1];
            NSString* time = [NSString stringWithFormat:@"%ld",timeStamp];
            NSMutableDictionary *valueDic = [countDic objectForKey: time];
            who = [valueDic objectForKey:@"to"];
            which = [[valueDic objectForKey:@"count"] intValue];
            NSData *userData = [[NSUserDefaults standardUserDefaults] objectForKey:K_USER_USERMESSAGE];
            MxmUserMessage *mxmUserModel = [NSKeyedUnarchiver unarchiveObjectWithData:userData];
            NSString *my = mxmUserModel.userName;
            
            NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"message.data"];
            if (![[NSFileManager defaultManager] fileExistsAtPath:file]) {
            }else{
                NSMutableDictionary *aDic = [[NSMutableDictionary alloc]initWithContentsOfFile:file];
                NSMutableArray *dataArr = [aDic objectForKey:my];
                
                for (int i = 0; i

你可能感兴趣的:(AFN上传视频)