ios - Upload a single video file to server -
i want upload video webservice server. video take uiimagepicker here code uiimagepicker:-
- (ibaction)btn_select:(id)sender { uiimagepickercontroller *imgpicker=[[uiimagepickercontroller alloc]init]; imgpicker.delegate=self; imgpicker.sourcetype=uiimagepickercontrollersourcetypesavedphotosalbum; imgpicker.mediatypes = [[nsarray alloc] initwithobjects:(nsstring *)kuttypemovie,nil]; [self presentviewcontroller:imgpicker animated:yes completion:nil];} - (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info { [picker dismissviewcontrolleranimated:yes completion:nil]; // dismiss image picker view controller nsstring *mediatype = [info objectforkey:uiimagepickercontrollermediatype]; if ([mediatype isequaltostring:(nsstring *)kuttypemovie]) { nsurl *mediaurl = [info objectforkey:uiimagepickercontrollermediaurl]; mpmovieplayercontroller *movieplayer = [[mpmovieplayercontroller alloc] initwithcontenturl:mediaurl]; movieplayer.shouldautoplay = no; nslog(@"%@",mediaurl); uiimage *thumbnail = [movieplayer thumbnailimageattime:0.0 timeoption:mpmovietimeoptionnearestkeyframe]; [img_out setimage:thumbnail]; //imageview uiimageview }
now want upload selected video button action server post method...
i have make string post:-
nsstring *post=[nsstring stringwithformat:@"uid=791986&category=game&description=asd&language=english&country=us&bobltags=asd&video=file:///users/mymac/library/developer/coresimulator/devices/3dd070c1-71de-4672-bac2-9b4e64f57d0a/data/containers/data/application/68e251cb-7c67-4559-a13d-91f222b9d0ee/tmp/trim.9467b9de-d7bf-43be-99da-366589746063.mov"];
now ahead. have lots of example didn't correctly kboundry , else...
give me code..
for posting video need use function after image picker delegate:
- (nsdata *)generatepostdatafordata:(nsdata *)uploaddata { // generate post header: nsstring *post = [nsstring stringwithcstring:"--aab03x\r\ncontent-disposition: form-data; name=\"upload[file]\"; filename=\"somefile\"\r\ncontent-type: application/octet-stream\r\ncontent-transfer-encoding: binary\r\n\r\n" encoding:nsasciistringencoding]; // post header int ascii format: nsdata *postheaderdata = [post datausingencoding:nsasciistringencoding allowlossyconversion:yes]; // generate mutable data variable: nsmutabledata *postdata = [[nsmutabledata alloc] initwithlength:[postheaderdata length] ]; [postdata setdata:postheaderdata]; // add image: [postdata appenddata: uploaddata]; // add closing boundry: [postdata appenddata: [@"\r\n--aab03x--" datausingencoding:nsasciistringencoding allowlossyconversion:yes]]; // return post data: return postdata; } - (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info{ //assign mediatype string nsstring *mediatype = [info objectforkey:uiimagepickercontrollermediatype]; //check media type string can determine if video if ([mediatype isequaltostring:@"public.movie"]){ nslog(@"got movie"); nsurl *videourl = [info objectforkey:uiimagepickercontrollermediaurl]; nsdata *webdata = [nsdata datawithcontentsofurl:videourl]; [self post:webdata]; [webdata release]; }
for post video use function:
- (void)post:(nsdata *)filedata { nslog(@"posting"); // generate postdata: nsdata *postdata = [self generatepostdatafordata: filedata]; nsstring *postlength = [nsstring stringwithformat:@"%d", [postdata length]]; // setup request: nsmutableurlrequest *uploadrequest = [[[nsmutableurlrequest alloc] initwithurl:[nsurl urlwithstring:@"http://www.example.com:3000/"] cachepolicy: nsurlrequestreloadignoringlocalcachedata timeoutinterval: 30 ] autorelease]; [uploadrequest sethttpmethod:@"post"]; [uploadrequest setvalue:postlength forhttpheaderfield:@"content-length"]; [uploadrequest setvalue:@"multipart/form-data; boundary=aab03x" forhttpheaderfield:@"content-type"]; [uploadrequest sethttpbody:postdata]; // execute reqest: nsurlconnection *conn=[[nsurlconnection alloc] initwithrequest:uploadrequest delegate:self]; if (conn) { // connection succeeded (even if 404 or other non-200 range returned). nslog(@"sucess"); uialertview *alert = [[uialertview alloc] initwithtitle:@"got server response" message:@"success" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [alert show]; [alert release]; } else { // connection failed (cannot reach server). nslog(@"fail"); } }
Comments
Post a Comment