The project needs to use the Post method in the HTTP protocol to upload the file, a little summary, the process of pasting out, convenient for later reference. There are two methods, the first is to use nsmutableurlrequest completely from zero to set up, can deepen the understanding of the HTTP protocol, the second is to directly use other people's encapsulated code, such as Afnetworking.
Method one, starting from 0 file upload
nsurl *url = [nsurl urlwithstring:@ "http://yo.diveinedu.com/upload.php"]; Nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url];request. httpmethod = @ "POST";//0. set delimiter nsstring *boundary = @ "A1b2c3d4e5f6"; [request setvalue:[nsstring stringwithformat:@ "multipart/form-data; boundary=%@", boundary ] forhttpheaderfield:@ "Content-type"]; NSLog (@ "%@", request.allhttpheaderfields); nsmutabledata *httpbody = [nsmutabledata data];//1. start delimiter [httpBody appendData:[[ nsstring stringwithformat:@ "--%@\r\n",  BOUNDARY] DATAUSINGENCODING:NSUTF8STRINGENCODING]];//2. Set content: tag name, file name, etc. [httpbody appenddata:[[nsstring stringwithformat:@] content-disposition: form-data; name=\ "filetoupload\", filename=\ "%@\" \ r \ n ", @" Mask0.png "] Datausingencoding:nsutf8stringencoding]]; Nsstring *path = [[nsbundle mainbundle] pathforresource:@ "Mask0" oftype:@ "PNG"]; NSLog (@ "%@", [self typeforpath:path]);//3. set content format [httpbody appenddata:[[nsstring stringwithformat:@ "content-type: %@\r\n\r\n", [self typeforpath:path]] datausingencoding: Nsutf8stringencoding]]; Nsstring *body = [[nsstring alloc] initwithdata:httpbody encoding: Nsutf8stringencoding]; NSLog (@ "%@", body);//4. add true content nsdata *data = [nsdata datawithcontentsoffile: Path]; [httpbody appenddata:data];//5. add end boundary [httpbody appenddata:[[nsstring stringwithformat:@] \ r\n--%@--\r\n ", boundary] datausingencoding:nsutf8stringencoding]];//6. Settings http bodyrequest . httpbody = httpbody;//7. Send Request [nsurlconnection sendasynchronousrequest:request queue:[ nsoperationqueue mainqueue] completionhandler:^ (Nsurlresponse *response, nsdata *data , nserror *conneCtionerror) { NSString *str = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; nslog (@ "%@", str);}];
Focus:
Note that you need to use the position of carriage return (\ r \ n) in the protocol, HTTP is a text line-based protocol that controls the format by carriage return line.
Note the setting of the delimiter (boundary), especially the (--) increase.
After setting the content should be the following (in order to see more clearly, the carriage return line is expressed in text):
Post/upload.php Http/1.1\r\nhost:yo.diveinedu.com\r\ncontent-type:multipart/form-data; Boundary=abcdefghigk\r\n\r\ncontent-disposition:form-data; Name= "Filetoupload"; Filename= "Mask0.png" \r\ncontent-type:image/png\r\n\r\n--abcdefghigk\r\n picture data \r\n--abcdefghigk--\r\n
Method two, using the method in Afnetworking
afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager]; manager.responseserializer = [afhttpresponseserializer serializer]; [manager post:@ "http://yo.diveinedu.com/upload.php" parameters:nil constructingbodywithblock:^ ( Id<afmultipartformdata> formdata) { //1. method one// NSError *error;// if (![ formdata appendpartwithfileurl:[nsurl fileurlwithpath:path] name:@ "FileToUpload" fileName: [path lastpathcomponent] mimetype:@ "Image/png" error:&error]) {// nslog (@ "error appending part: %@", Error);// } //2. method two NSData *data = [NSData datawithcontentsoffile:path]; [formdata appendpartwithfiledata:data name:@ " Filetoupload " filename:[path lastpathcomponent] mimetype:@" Image/png "];} success:^ (Afhttprequestoperation *operation, id responseobject) { NSData *data = (nsdata *) responseobject; nsstring *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; nslog (@ "%@", str);} failure:^ (Afhttprequestoperation *operation, nserror *error) { nslog (@ "%@", error);}];
When you are programming your network, you can use a grab kit like Wireshark to assist with debugging. You can clearly see whether the data sent or received is correct.
Finally, I wish you a happy children's Day ~ ~
Http://io.diveinedu.com
Http://www.diveinedu.com
Http://bbs.diveinedu.com
Https://github.com/DiveinEdu-CN
Two ways to upload a file using the Post method