1

I am uploading a video to server with post method. but there is problem that it is not received by server.

her my code for posting a video.

NSString *post=[NSString stringWithFormat:@"domain=%@,key=%@,email=%@,password=%@,category=%@,sub_category=%@,title=%@,photoimg-videos=%@,keywords=%@,language=%@,country=%@",domain,key,emailgetinvid,passgetinvid,category,btn_ctgry.currentTitle,txt_bobltag.text,videopathget,txt_description.text,btn_lang.currentTitle,btn_cntry.currentTitle];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[post length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"my url"]cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0];

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];

[request setHTTPMethod:@"POST"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];

this code is not working for me. i am getting that video from image picker and convert it to url.

convert that video in fileurl is good or not or it should be in data?

give me solution code.

Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
  • Please check the following urls. http://stackoverflow.com/questions/20088969/how-to-upload-video-to-server-from-iphone http://www.oodlestechnologies.com/blogs/Record-and-Upload-videos-to-server-in-iOShttp://stackoverflow.com/questions/1065628/uploading-video-with-iphone – aBilal17 Jul 16 '15 at 05:40
  • Check this as well http://stackoverflow.com/questions/8564833/ios-upload-image-and-text-using-http-post – aBilal17 Jul 16 '15 at 05:42

1 Answers1

0

Use AFNetworking because its easy and here the code goes for AFNetworking.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

AFHTTPRequestOperation *operation = [manager POST:@"Your url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:[NSData dataWithContentsOfFile:@"This will be name with path of the file you saved in directory"] name:@"the key by which you are receiving the file in server" fileName:@"The name you want for the file when its received in server." mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    APILog(@"response - %@",responseObject);
    success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    APILog(@"\nresponse -  = %@",operation.responseString);
    APILog(@"\nresponse description - %@",error.description);
    success(nil);
}];

[operation start];
Mahesh Agrawal
  • 3,348
  • 20
  • 34