使用afnetworking 3.0将应用程序视频上传到服务器

时间:2016-06-29 05:23:25

标签: ios video afnetworking

我将尝试使用afnetworking上传视频。我将在我的项目中拍摄一个视频,而不是在服务器上传。我只想在服务器上传视频。

这是我的代码

- (IBAction)uplosd:(id)sender {
      // name of the image
    NSString *videoURL = [[NSBundle mainBundle] pathForResource:@"sample.mp4" ofType:@"mov"];
    NSData *videoData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:videoURL]];
      // convert your image into data

    NSString *urlString = [NSString stringWithFormat:@"url"];  // enter your url to upload

    // AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // allocate AFHTTPManager
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

    [manager POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {  // POST DATA USING MULTIPART CONTENT TYPE
        [formData appendPartWithFileData:videoData
                                    name:@"image"
                                fileName:@"video" mimeType:@"video/quicktime"];   // add image to formData
        [formData appendPartWithFormData:[@"sdsd" dataUsingEncoding:NSUTF8StringEncoding]
                                    name:@"key1"];
         // add your other keys !!!

    } progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"Response: %@", responseObject);    // Get response from the server



    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"Error: %@", error);   // Gives Error

    }];

}

这里sample.mp4是视频存储在我的project.pls帮帮我

1 个答案:

答案 0 :(得分:1)

1)

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
     NSData *tempData = nil;

            NSString *path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"mp4"];
        NSData *data = [NSData dataWithContentsOfFile:path];



            AFHTTPRequestOperation *operation = [manager POST:strRequestUrl parameters:pdictParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
                                                 {

                                                     if (data)
                                                     {
     [formData appendPartWithFileData:tempData name:@"video_file" fileName:[path lastPathComponent] mimeType:@"video/mp4"];
          }
                                         } success:^(AFHTTPRequestOperation *operation, id responseObject){
          } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        }];

            [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

                NSLog(@"Upload Progress %lld",totalBytesWritten*100/totalBytesExpectedToWrite);


     float fltProgress = (float)totalBytesWritten/(float)totalBytesExpectedToWrite;

                NSLog(@"%f",fltProgress);

            }];
            // [operation setInputStream:[NSInputStream inputStreamWithURL:[NSURL URLWithString:strRequestUrl]]];
            [operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
                [operation pause];
            }];

2)

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSDictionary *parameters = @{@"foo": @"bar"};
NSString *path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"mp4"];
    NSURL *filePath = [NSURL fileURLWithPath:path];
    [manager POST:strRequestUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileURL:filePath name:@"video" error:nil];
    } progress:nil success:^(NSURLSessionDataTask *operation, id responseObject) {
        NSLog(@"Success: %@", responseObject);
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];