EmptyStack
EmptyStack

Reputation: 51374

AFNetworking - Upload video along with other parameters

I want to upload video to web service along with some other parameters. I want to upload userID, videoID and video to web service. While uploading, all the parameters other than video is being sent to web service. I've checked at web service end, and the video is not coming with the request. I am using the following code.

- (void)uploadVideoAtLocalPath:(NSString *)videoPath videoID:(NSString *)videoID userID:(NSString *)userID {

    NSString *strServerURL = @"www.mysite.com/user/uploadVideo";
    NSURL *URL = [NSURL URLWithString:strServerURL];
    AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:URL];

    NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {

        // userID
        NSData *userIDData = [userID dataUsingEncoding:NSUTF8StringEncoding];
        [formData appendPartWithFormData:userIDData name:@"userID"];

        // videoID
        NSData *videoIDData = [videoID dataUsingEncoding:NSUTF8StringEncoding];
        [formData appendPartWithFormData:videoIDData name:@"videoID"];

        // video
        NSData *videoData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:videoPath]];
        [formData appendPartWithFileData:videoData name:@"video" fileName:@"video.mov" mimeType:@"video/quicktime"];
    }];

    [request setURL:URL];
    [request setTimeoutInterval:60.0];
    [request setHTTPMethod:@"POST"];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [AFHTTPRequestOperation addAcceptableStatusCodes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(100, 500)]];
    [operation setCompletionBlockWithSuccess: ^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Response String: %@", operation.responseString);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure: %@", error);
    }];

    [client enqueueHTTPRequestOperation:operation];
}

Could anyone let me know whether I am doing it correct? If not, could anyone please tell me how to upload video to web service along with other parameters?

Thanks Everyone!

Upvotes: 4

Views: 3609

Answers (1)

Praveenkumar
Praveenkumar

Reputation: 24476

I'm not well in this method. I had the same issue. But, i have fixed it like mixing of POST & GET methods. I just sent my parameters as GET method like below -

NSString *strServerURL = [NSString stringWithFormat:@"www.mysite.com/user/uploadVideo&userID=%d&videoID=%d", 1, 55];

and, sent my video data in POST method as per your method -

NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {

    // video
    NSData *videoData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:videoPath]];
    [formData appendPartWithFileData:videoData name:@"video" fileName:@"video.mov" mimeType:@"video/quicktime"];
}];

You better try to modify your webservice and try like above way. It should works.

Cheers!

Upvotes: 1

Related Questions