objective c - How to upload a file to server stored in my documents folder in ios? -


i want upload files (of different formats) stored in application documents folder. listed files in tableview on selecting row trying upload file in row. here code

- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath  {    nsstring *urlstring; if (indexpath.section == 0)     {        fileurltoupload = [nsurl fileurlwithpath:[[nsbundle mainbundle] pathforresource:documents[indexpath.row] oftype:nil]]; } else { fileurltoupload = [self.documenturls objectatindex:indexpath.row];  } [self setupdocumentcontrollerwithurl:fileurltoupload];  urlstring =   [[fileurltoupload path] lastpathcomponent]; printf("\n string :%s",[urlstring utf8string]); [self publishtoserver:urlstring];    -(void)publishtoserver:(nsstring *)str   { if([str length] > 0)  {      nsstring *urlpath=@"";     urlpath= str;     nsstring *urlstring = @"http://162.108.0.8:8080/path/upload_path.jsp?";     nsmutableurlrequest *request =[[[nsmutableurlrequest alloc] init] autorelease];     [request seturl:[nsurl urlwithstring:urlstring]];     [request sethttpmethod:@"post"];      nsstring *boundary = [nsstring stringwithstring:@"---------------------------14737809831466499882746641449"];     nsstring *contenttype = [nsstring stringwithformat:@"multipart/form-data; boundary=%@",boundary];     [request addvalue:contenttype forhttpheaderfield: @"content-type"];      nsmutabledata *postbody = [nsmutabledata data];       [postbody appenddata:[[nsstring stringwithformat:@"--%@\r\n",boundary] datausingencoding:nsutf8stringencoding]];     [postbody appenddata:[[nsstring stringwithstring:@"content-disposition: form-data; name=\"path\"\r\n\r\n"] datausingencoding:nsutf8stringencoding]];     [postbody appenddata:[[nsstring stringwithstring:urlpath] datausingencoding:nsutf8stringencoding]];     [postbody appenddata:[[nsstring stringwithformat:@"\r\n--%@\r\n",boundary] datausingencoding:nsutf8stringencoding]];  [postbody appenddata:[[nsstring stringwithformat:@"\r\n--%@--",boundary] datausingencoding:nsasciistringencoding allowlossyconversion:yes]];      [request sethttpbody:postbody];      nslog(@"request ===  %@",request);      nsdata *returndata =nil;     returndata= [nsurlconnection sendsynchronousrequest:request returningresponse:nil error:nil];      nslog(@"returndata ===  %@",returndata);      nsstring * string=[[nsstring alloc]initwithdata:returndata encoding:nsutf8stringencoding];   } 

1) when trying post file simulator null value storing in server database , receiving "upload successfully" response.
2) when trying post file device not getting null value in server.

please me.

thank you

not sure file types, images can upload, btw, have tried possible examples here, none worked (i had $_files array empty), had construct own, based on examples , works:

- (void) uploadlog :(nsstring *) filepath { nsdata *data = [[nsdata alloc] initwithcontentsoffile:filepath]; nsstring *urlstring =[nsstring stringwithformat:@"http://www.yoursite.com/accept.php"]; // use please use real website link. nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init]; [request seturl:[nsurl urlwithstring:urlstring]]; [request sethttpmethod:@"post"];  nsstring *boundary = @"_187934598797439873422234"; nsstring *contenttype = [nsstring stringwithformat:@"multipart/form-data; boundary=%@",boundary]; [request setvalue:contenttype forhttpheaderfield: @"content-type"]; [request setvalue:@"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" forhttpheaderfield:@"accept"]; [request setvalue:@"mozilla/5.0 (macintosh; intel mac os x 10_7_5) applewebkit/536.26.14 (khtml, gecko) version/6.0.1 safari/536.26.14" forhttpheaderfield:@"user-agent"]; [request setvalue:@"http://google.com" forhttpheaderfield:@"origin"];  nsmutabledata *body = [nsmutabledata data]; [body appenddata:[[nsstring stringwithformat:@"content-length %d\r\n\r\n", [data length] ] datausingencoding:nsutf8stringencoding]]; [body appenddata:[[nsstring stringwithformat:@"\r\n--%@\r\n",boundary] datausingencoding:nsutf8stringencoding]]; [body appenddata:[[nsstring stringwithformat:@"content-disposition: form-data; name=\"picture\"; filename=\"%@.png\"\r\n", @"newfile"] datausingencoding:nsutf8stringencoding]];  [body appenddata:[@"content-type: application/octet-stream\r\n\r\n" datausingencoding:nsutf8stringencoding]];  [body appenddata:[nsdata datawithdata:data]];  [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); } 

and here php part works me (sorry, not familiar jsp):

<?php     $target_path = "./uploads/";        $target_path = $target_path . basename( $_files['picture']['name']);        if(move_uploaded_file($_files['picture']['tmp_name'], $target_path)) {       echo "the file ".  basename( $_files['picture']['name'])." has been uploaded";       } else{       echo "there error uploading file, please try again!";       }   ?> 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -