Note: When a file is uploaded using a POST request, the data to be uploaded is usually stored in the request body. This article describes how to implement a file upload in iOS development without the help of a third-party framework.
Because of the complexity of the process, only some of the key code is posted.
Key code for the host controller:
Yyviewcontroller.m
1 #import "YYViewController.h"2 3 #defineYyencode (str) [STR datausingencoding:nsutf8stringencoding]4 5 @interfaceYyviewcontroller ()6 7 @end8 9 @implementationYyviewcontrollerTen One- (void) Viewdidload A { - [Super Viewdidload]; - //additional setup after loading the view, typically from a nib. the } - -- (void) Upload: (NSString *) name filename: (nsstring *) filename mimeType: (NSString *) mimeType data: (NSData *) Data parmas: ( Nsdictionary *)params - { + //File Upload -Nsurl *url = [Nsurl urlwithstring:@"Http://192.168.1.200:8080/YYServer/upload"]; +Nsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url]; ARequest. HttpMethod =@"POST"; at - //set the request body -Nsmutabledata *body =[Nsmutabledata data]; - - /*************** file parameter ***************/ - //flag for parameter start in[Body Appenddata:yyencode (@"--yy\r\n")]; - //Name: Specify parameter name (must be consistent with server side) to //FileName : File name +NSString *disposition = [NSString stringWithFormat:@"content-disposition:form-data; name=\"%@\"; filename=\ "%@\" \ r \ n", name, filename]; - [Body appenddata:yyencode (disposition)]; theNSString *type = [NSString stringWithFormat:@"Content-type:%@\r\n", MimeType]; * [Body Appenddata:yyencode (type)]; $ Panax Notoginseng[Body Appenddata:yyencode (@"\ r \ n")]; - [Body appenddata:data]; the[Body Appenddata:yyencode (@"\ r \ n")]; + A /*************** general parameter ***************/ the[paramsenumeratekeysandobjectsusingblock:^ (IDKeyIDobj, BOOL *stop) { + //flag for parameter start -[Body Appenddata:yyencode (@"--yy\r\n")]; $NSString *disposition = [NSString stringWithFormat:@"content-disposition:form-data; name=\"%@\"\ r \ n", key]; $ [Body appenddata:yyencode (disposition)]; - -[Body Appenddata:yyencode (@"\ r \ n")]; the [Body appenddata:yyencode (obj)]; -[Body Appenddata:yyencode (@"\ r \ n")];Wuyi }]; the - /*************** parameter End ***************/ Wu //yy--\r\n -[Body Appenddata:yyencode (@"--yy--\r\n")]; AboutRequest. Httpbody =body; $ - //set the request header - //the length of the request body -[Request Setvalue:[nsstring stringWithFormat:@"%zd", Body.length] Forhttpheaderfield:@"Content-length"]; A //declare that the POST request is a file upload +[Request SetValue:@"multipart/form-data; Boundary=yy"Forhttpheaderfield:@"Content-type"]; the - //Send Request $[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) { the if(data) { theNsdictionary *dict =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:nil]; theNSLog (@"%@", dict); the}Else { -NSLog (@"Upload failed"); in } the }]; the } About the- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event the { the //Socket Implementation Breakpoint upload + - //apache-tomcat-6.0.41/conf/web.xml find mimeType for files the //UIImage *image = [UIImage imagenamed:@ "test"];Bayi //NSData *filedata = uiimagepngrepresentation (image); the //[Self upload:@ "file" filename:@ "Test.png" mimetype:@ "image/png" Data:filedata parmas:@{@ "username": @ "123"}]; the - //send a request to a local file -Nsurl *fileurl = [[NSBundle mainbundle] Urlforresource:@"Itcast.txt"Withextension:nil]; theNsurlrequest *request =[Nsurlrequest Requestwithurl:fileurl]; theNsurlresponse *repsonse =Nil; theNSData *data = [Nsurlconnection sendsynchronousrequest:request returningresponse:&repsonse Error:nil]; the - //Get MimeType theNSLog (@"%@", Repsonse. MIMEType); the[Self Upload:@"file"FileName@"Itcast.txt"Mimetype:repsonse. MIMEType Data:data parmas:@{ the @"username":@"999",94 @"type":@"XML"}]; the } the the @end
Additional notes:
File upload request data format
MimeType of some files
iOS Development Network-File upload