Code
-
-(Ibaction) uploadaction :( ID ) Sender {
Nsstring * urlstring = self. urltextfield. text;
// Nsstring * poststr = @"";
Nsdata * imgdata = uiimagejpegrepresentation (self. previewimageview. image, 0.9f );
Nsstring * boundary = @" 0 xkhtmlboundary " ;
Nsstring * contenttype = [nsstring stringwithformat: @" Multipart/form-data; boundary =% @ " , Boundary, nil];
Nsurl * url = [nsurl urlwithstring: urlstring];
Nsmutableurlrequest * request = [nsmutableurlrequest requestwithurl: url];
[Request addvalue: contenttype forhttpheaderfield: @" Content-Type " ];
[Request sethttpmethod: @" Post " ];
Nsmutabledata * Body = [nsmutabledata data];
Nsarray * _ Params = @[ @" Submitter " , @" Username " ];
// Add submit formdata
For (Nsstring * Param In _ Params ){
[Body appenddata: [[nsstring stringwithformat: @" -- % @ \ R \ n " , Boundary] datausingencoding: nsutf8stringencoding];
[Body appenddata: [[nsstring stringwithformat: @" Content-Disposition: Form-data; name = \ " % @\ " \ R \ n " , Param] datausingencoding: nsutf8stringencoding];
[Body appenddata: [[nsstring stringwithformat: @" % @ \ R \ n " , Param] datausingencoding: nsutf8stringencoding];
}
[Body appenddata: [[nsstring stringwithformat: @" \ R \ n -- % @ \ r \ n " , Boundary] datausingencoding: nsutf8stringencoding];
[Body appenddata :[ @" Content-Disposition: Form-data; name = \ " Userfile \ " ; Filename = \ "iphonefile.jpg \" \ r \ n " Datausingencoding: nsutf8stringencoding];
[Body appenddata :[ @" Content-Type: Application/octet-stream \ r \ n " Datausingencoding: nsutf8stringencoding];
// [Body appenddata: [@ "submitter: fromiphone" datausingencoding: nsutf8stringencoding];
[Body appenddata: [nsdata datawithdata: imgdata];
[Body appenddata: [[nsstring stringwithformat: @" \ R \ n -- % @ -- \ r \ n " , Boundary] datausingencoding: nsutf8stringencoding];
[Request sethttpbody: body];
Nsoperationqueue * queue = [[nsoperationqueue alloc] init];
[Nsurlconnection sendasynchronousrequest: Request queue: queue
Completionhandler: ^ (nsurlresponse * respone,
Nsdata * data,
Nserror * error)
{
If ([Data Length]> 0 & Error = nil ){
Nsstring * jsonstring = [[nsstring alloc] initwithdata: data encoding: nsutf8stringencoding];
// [Self. respondtextview settext: jsonstring];
Nslog ( @" Data: % @ " , Jsonstring );
// [Self initialize mselecw.mainthread: @ selector (setrespondtext :) withobject: jsonstring waituntildone: Yes modes: Nil];
// [Self defined mselecw.mainthread: @ selector (setrespondtext :) withobject: Data waituntildone: No];
}
}
];
}