4
我想将视频和其他一些参数一起上传到网络服务。我想上传用户ID,视频ID和视频到网络服务。上传时,视频以外的所有参数都将发送到Web服务。我在网络服务结束时检查过,并且视频没有与请求一起发送。我正在使用下面的代码。AFNetworking - 上传视频以及其他参数
- (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];
}
任何人都可以让我知道我是否做得正确吗?如果没有,有谁可以请告诉我如何将视频和其他参数一起上传到Web服务?
谢谢大家!
你有没有检查'videoData'是否为零? –
你可以尝试将用户ID和videoID放入NSDictionary中,并将其放在方法multipartFormRequestWithMethod中的参数中,而不是放入nil? –
@AtilaH:你好,videoData不是零。我检查过了。谢谢。 – EmptyStack