AFNetworking - download video along with other parameters

I want to upload a video to a web service along with some other options. I want to upload userID , videoID and video to a web service. When downloading, all parameters other than video are sent to the web service. I checked at the end of the web service and the video does not go 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]; } 

Can someone tell me if I am doing this correctly? If not, can someone tell me how to upload video to a web service along with other options?

Thanks everyone!

+4
source share
1 answer

I am not good at this method. I had the same problem. But I fixed it as mixing POST and GET methods. I just sent my parameters as a GET method as shown below -

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

and sent my video data in the POST method according to 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"]; }]; 

Better try changing your web service and try as described above. It should work.

Hooray!

+1
source

Source: https://habr.com/ru/post/1488861/


All Articles