在我的应用程序中,我正在iPad中录制视频,我想将其上传到我的服务器。请告诉我如何实现这一目标。
答案 0 :(得分:1)
在其中创建带有文件对象的请求字典,稍后postWith:function将操纵您的视频绑定任务
NSDictionary *requestDict = [NSDictionary dictionaryWithObjectsAndKeys:@"FirstObjectValue",@"FirstKey",
@"Second Object",@"Second Key",
@"myFileParameterToReadOnServerSide",@"file", nil]; // This line indicate ,POST data has file to attach
[self postWith:requestDict];
如果您要发送视频,然后在您的字典中添加“文件”键,那么函数将从您想要POST的对象的字典中读取所有参数,以确定有一些文件要发送请求
- (void)postWith:(NSDictionary *)post_vars
{
#warning Add your Webservice URL here
NSString *urlString = [NSString stringWithFormat:@"YourHostString"];
NSURL *url = [NSURL URLWithString:urlString];
NSString *boundary = @"----1010101010";
// define content type and add Body Boundry
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSEnumerator *enumerator = [post_vars keyEnumerator];
NSString *key;
NSString *value;
NSString *content_disposition;
while ((key = (NSString *)[enumerator nextObject])) {
if ([key isEqualToString:@"file"]) {
value = (NSString *)[post_vars objectForKey:key];
// *** Write Your Image Name Here ***
// Covnert Video to Data and bind to your post request
#warning Add your Video From Path
NSString *videoFilePath = [[NSBundle mainBundle] pathForResource:@"MyVideo" ofType:@"mp4"];
NSData *postData = [NSData dataWithContentsOfFile:videoFilePath];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\";\r\nfilename=\"testUploadFile.mp4\"\r\n\r\n",value] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:postData];
} else {
value = (NSString *)[post_vars objectForKey:key];
content_disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key];
[body appendData:[content_disposition dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
//Close the request body with Boundry
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:@"%d", body.length] forHTTPHeaderField: @"Content-Length"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
}