使用post方法将视频上传到服务器

时间:2015-07-16 05:29:00

标签: ios objective-c xcode post

我使用post方法将视频上传到服务器。 但是有问题是服务器没有收到它。

她发布视频的代码。

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

此代码对我不起作用。 我从图像选择器获取该视频并将其转换为网址。

转换fileurl中的视频是好还是不好,或者它应该在数据中?

给我解决方案代码。

1 个答案:

答案 0 :(得分:0)

使用AFNetworking,因为它很容易,这里的代码用于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];