4

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!

4

1 回答 1

1

我不擅长这种方法。我遇到过同样的问题。但是,我已经修复了它,就像混合POSTGET方法一样。我刚刚将我的参数作为GET方法发送,如下所示 -

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

并且,按照您的方法以POST方法发送我的视频数据-

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

您最好尝试修改您的网络服务并尝试像上面的方式。它应该有效。

干杯!

于 2013-09-16T04:47:49.937 回答