The above is the content of the upload of the package HTML input file tag.
Just simulate the header and body of HTTP above to be successful.
Overall Description:
Post upload file, in the HTTP body with parameters, parameter format, according to RFC1867 protocol requirements written as follows
In the HTTP header, define the boundary, the value of boundary is the body inside the flag separating the parameters.
Content-type:multipart/form-data; boundary=----------Llcuploadtaskwithrequest
Assuming post in the past, two parameters user= "Loganv" &pwd= "123456" followed by a binary file, the service side through the field ' file ' to get the file stream, the file name is called Filename.png.
------------llcuploadtaskwithrequestcontent-disposition:form-data; Name="User"
Loganv------------llcuploadtaskwithrequestcontent-disposition:form-data; Name=the PWD"
123456------------llcuploadtaskwithrequestcontent-disposition:form-data; Name="file"; Filename="Filename.png"Content-type:application/octet-STREAMŸØŸÀJFIFHHŸÁ¼EXIFMM*JR (? I (here is uploaded binary file data)
Look at this body as a pair of the whole.
------------llcuploadtaskwithrequestcontent-disposition:form-data; name= "user"Loganv
The first line: the value of the boundary defined in the header, preceded by "--", with "\ r \ n" Ending
Second line: Defines the key user of the parameter, ending with "\ r \ n"
Third line: "\ r \ n"
Line four: The value of the parameter 123456, ending with "\ r \ n"
If the parameter is a file, you need to add a line below the lower two line to define the file's type MIME type
IOS implementation uploads, swift2.0 main code
The first step: Customizing the boundary partition flag, adding a content-type to the header;
Let post_boundary = "----------llcuploadtaskwithrequest"
Let reqq = Nsmutableurlrequest (Url:nsurl (string: "http://172.16.0.199/upload/uploadIpa.php ")! )
reqq. HttpMethod = "POST"
Reqq.setvalue (post_boundary, Forhttpheaderfield: "content-type")
The second step: stitching the HTTP body needs to post the past parameters, with the binary file; The server gets the file stream through the field ' file ';
Let session = Nsurlsession (Configuration:NSURLSessionConfiguration.defaultSessionConfiguration ())
var paramstr = ""
if param! = nil{
For (K, v) in param! {
Paramstr + = Uploadpostparaminbody (k, namevalue:v.description)
}
}
Paramstr + = uploadpostparaminbody ("file", FileName: "Filename.png")
Let boundary = paramstr.datausingencoding (nsutf8stringencoding)?. Mutablecopy () as? Nsmutabledata
Let pdata = NSData (data:multipartformdata!)
Let Boundary_end = ("\r\n--" + post_boundary + "--"). Datausingencoding (nsutf8stringencoding)
Boundary?. AppendData (pdata)
Boundary?. AppendData (boundary_end!)
Method of stitching parameters
//----Uploadtaskwithrequest Upload /*Name:post The name of the parameter inside the body, the server obtains the corresponding parameter value according to the name; FileName: The name of the file, such as Logo.png; Mimetyp: For Application/oc Tet-stream represents any binary flow can be, picture transmission image/jpeg;*/ PublicFunc uploadpostparaminbody (name:string, filename:string ="", namevalue:string ="", mimetype:string ="Application/octet-stream"),string{var content="--"+ Llchttprequest.post_boundary +"\ r \ n" iffilename = =""{content+="content-disposition:form-data; name=\ "\ (name) \" \r\n\r\n"; Content+="\ (namevalue) \ r \ n"; } Else{content+="content-disposition:form-data; name=\ "\ (name) \"; filename=\ "\ (filename) \ \ \ \ \ \ \ \ \ \ \ \ \ \ \"; Content+="Content-type: \ (MIMEtype) \r\n\r\n"; } returncontent}
Step Three: Request
Let task =session.uploadtaskwithrequest (req.request, fromdata:boundary) {(Obj:nsdata?, Resp:nsurlresponse?, Err:nserror?) -Voidinch ifLet resphtml = Resp as?Nshttpurlresponse {ifResphtml.statuscode = = $ { ifObj! =nil{var resu:anyobject? = String (data:obj!, encoding:nsutf8stringencoding)ifLet JSON =Try? Nsjsonserialization.jsonobjectwithdata (obj!, Options:. allowfragments) {ResU=JSON}} } } Else{fail (RESP:RESP, Error:err)}ifErr! =nil{fail (RESP:RESP, Error:err)}} task.resume ()
Finished!
Attach the PHP server to simply accept the file upload code
<?PHP $text= Isset ($_request['text']) ? $_request['text'] :""; Echo'success<br>'; if($_files["file"]["Error"] >0) {echo"Error:". $_files["file"]["Error"] ."<br/>"; } elseif ($_files["file"]["size"] > -*1024x768*1024x768) {echo'file too big'; } Else{echo"Upload:". $_files["file"]["name"] ."<br/>"; Echo"Type:". $_files["file"]["type"] ."<br/>"; Echo"Size:". ($_files["file"]["size"] /1024x768) ."kb<br/>"; Echo"Stored in:". $_files["file"]["Tmp_name"] .'<br/>'; $isipa=false; $UPLOADSRC='.. /download/ipa/'. $_files["file"]["name"]; if(Preg_match ('/\.ipa$/', $_files["file"]["name"]) {$isipa=true; $UPLOADSRC='.. /download/ipa/'.'Test01.ipa'; } if(File_exists ($UPLOADSRC) &&$isipa) { //echo $_files["file" ["Name"]. "already exists.";Rename ($UPLOADSRC,'.. /download/ipa/'. Date ('y-m-d h:i:s').'. IPA'); } move_uploaded_file ($_files["file"]["Tmp_name"], $UPLOADSRC); Echo"Stored in:". $UPLOADSRC.'<br>'; Echo'Upload Success! '; }
iOS upload file, about HTTP upload protocol-rfc1867