我想将视频上传到网络服务以及其他一些参数。我想将用户ID , videoID 和视频上传到网络服务。上传时,除视频之外的所有参数都将发送到Web服务。我已经在网络服务端检查了,视频没有附带请求。我使用以下代码。
- (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];
}
有人能告诉我这是否正确吗?如果没有,有人可以告诉我如何将视频上传到网络服务以及其他参数吗?
谢谢大家!
答案 0 :(得分:1)
我对这种方法不太满意。我遇到过同样的问题。但是,我把它固定好像混合了 POST &amp; GET 方法。我刚刚将参数发送为 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"];
}];
您最好尝试修改您的网络服务并尝试上述方式。它应该有用。
干杯!