iOS Development Web-File upload
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
#import"YYViewController.h"#defineYyencode (str) [STR datausingencoding:nsutf8stringencoding]@interface Yyviewcontroller () @end @implementation Yyviewcontroller- (void) viewdidload{[Super Viewdidload]; //additional setup after loading the view, typically from a nib.}- (void) Upload: (NSString *) name filename: (nsstring *) filename mimeType: (NSString *) mimeType data: (NSData *) Data parmas: ( Nsdictionary *)params{ //File UploadNsurl *url = [Nsurl urlwithstring:@"Http://192.168.1.200:8080/YYServer/upload"]; Nsmutableurlrequest*request =[Nsmutableurlrequest Requestwithurl:url]; Request. HttpMethod=@"POST"; //set the request bodyNsmutabledata *body =[Nsmutabledata data]; /*************** file parameter ***************/ //flag for parameter start[Body Appenddata:yyencode (@"--yy\r\n")]; //Name: Specify parameter name (must be consistent with server side)//FileName : File nameNSString *disposition = [NSString stringWithFormat:@"content-disposition:form-data; name=\"%@\"; filename=\ "%@\" \ r \ n", name, filename]; [Body Appenddata:yyencode (disposition)]; NSString*type = [NSString stringWithFormat:@"Content-type:%@\r\n", MimeType]; [Body Appenddata:yyencode (type)]; [Body Appenddata:yyencode (@"\ r \ n")]; [Body appenddata:data]; [Body Appenddata:yyencode (@"\ r \ n")]; /*************** general parameter ***************/ [paramsenumeratekeysandobjectsusingblock:^ (ID key, id obj, 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")]; [Body Appenddata:yyencode (obj)]; [Body Appenddata:yyencode (@"\ r \ n")]; }]; /*************** parameter End ***************/ //yy--\r\n[Body Appenddata:yyencode (@"--yy--\r\n")]; Request. Httpbody=body; //set the request header//the length of the request body[Request Setvalue:[nsstring stringWithFormat:@"%zd", Body.length] Forhttpheaderfield:@"Content-length"]; //declare that the POST request is a file upload[Request SetValue:@"multipart/form-data; Boundary=yy"Forhttpheaderfield:@"Content-type"]; //Send Request[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) { if(data) {Nsdictionary*dict =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:nil]; NSLog (@"%@", Dict); } Else{NSLog (@"Upload failed"); } }];}- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{ //Socket Implementation Breakpoint upload//apache-tomcat-6.0.41/conf/web.xml find mimeType for files//UIImage *image = [UIImage imagenamed:@ "test"];//NSData *filedata = uiimagepngrepresentation (image);//[Self upload:@ "file" filename:@ "Test.png" mimetype:@ "image/png" Data:filedata parmas:@{@ "username": @ "123"}]; //send a request to a local fileNsurl *fileurl = [[NSBundle mainbundle] Urlforresource:@"Itcast.txt"Withextension:nil]; Nsurlrequest*request =[Nsurlrequest Requestwithurl:fileurl]; Nsurlresponse*repsonse =Nil; NSData*data = [Nsurlconnection sendsynchronousrequest:request returningresponse:&Repsonse Error:nil]; //Get MimeTypeNSLog (@"%@", Repsonse. MIMEType); [Self Upload:@"file"FileName@"Itcast.txt"Mimetype:repsonse. MIMEType Data:data parmas:@{ @"username":@"999", @"type":@"XML"}];} @end
Additional notes:
File upload request data format
MimeType of some files
iOS Development Web-File upload