iOS Development Web-File upload
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
1#import"YYViewController.h"23#define YYENCODE (str) [STR datausingencoding:nsutf8stringencoding]45@interfaceYyviewcontroller ()67@end89@implementationYyviewcontroller1011-(void) Viewdidload12{13[Super Viewdidload];14//Additional setup after loading the view, typically from a nib.15}1617-(void) Upload: (NSString *) name filename: (nsstring *) filename mimeType: (NSString *) mimeType data: (NSData *) Data parmas: ( Nsdictionary *)Params18{19//File UploadNsurl *url = [Nsurl urlwithstring:@"Http://192.168.1.200:8080/YYServer/upload"];Nsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url];Request. HttpMethod =@"POST";2324//Set the request bodyNsmutabledata *body =[Nsmutabledata data];2627/*File parameter ***************/28//Flag for parameter start[Body Appenddata:yyencode (@"--yy\r\n")];30//Name: Specify parameter name (must be consistent with server side)31//FileName: File nameNSString *disposition = [NSString stringWithFormat:@"Content-disposition:form-data; Name=\"%@\"; Filename=\ "%@\" \ r \ n", name, filename];33[Body Appenddata:yyencode (disposition)];NSString *type = [NSString stringWithFormat:@"Content-type:%@\r\n", MimeType];35[Body Appenddata:yyencode (type)];36PNS [Body Appenddata:yyencode (@"\ r \ n")];38[Body appenddata:data];[Body Appenddata:yyencode (@"\ r \ n")];4041/*General parameter ***************/42 [Params enumeratekeysandobjectsusingblock:^ (ID Key,ID obj, BOOL *Stop) {43//Flag for parameter start[Body Appenddata:yyencode (@"--yy\r\n")];NSString *disposition = [NSString stringWithFormat:@"Content-disposition:form-data; Name=\"%@\"\ r \ n", key];46[Body Appenddata:yyencode (disposition)];47[Body Appenddata:yyencode (@"\ r \ n")];49[Body Appenddata:yyencode (obj)];[Body Appenddata:yyencode (@"\ r \ n")];51}];5253/*Parameter End ***************/54//yy--\r\n[Body Appenddata:yyencode (@"--yy--\r\n")];Request. Httpbody =Body5758//Set the request header59//The length of the request body[Request Setvalue:[nsstring stringWithFormat:@"%zd", Body.length] Forhttpheaderfield:@"Content-length"];61//Declare that the POST request is a file upload[Request SetValue:@"Multipart/form-data; Boundary=yy"Forhttpheaderfield:@"Content-type"];6364//Send Request[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {if (data) {nsdictionary *dict = [Nsjsonserializati On Jsonobjectwithdata:data options:nsjsonreadingmutableleaves error:nil];68 NSLog (@ "%@", dict), or else {NSLog (@ " Upload failed ");}72}];73}74-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event76 {//Socket Implementation Breakpoint Upload 78 79// Apache-tomcat-6.0.41/conf/web.xml Find File MimeType80//UIImage *image = [UIImage imagenamed:@ "test"];81//NSData *filedat A = uiimagepngrepresentation (image);/[Self upload:@ "file" filename:@ "Test.png" mimetype:@ "image/png" Data: Filedata parmas:@{@ "username": @ "123"}];83 84//Send a request to a local file. nsurl *fileurl = [[NSBundle mainbundle] urlforresource:@ "I Tcast.txt "withextension:nil];86 nsurlrequest *request = [Nsurlrequest requestwithurl:fileurl];87 NSURLResponse * Repsonse = nil;88 NSData *data = [Nsurlconnection SendsynchronouSrequest:request returningresponse:&repsonse error:nil];89 90//Get mimeType91 NSLog (@ "%@", repsonse. MIMEType); [Self upload:@ "file" filename:@ "Itcast.txt" mimetype:repsonse. MIMEType data:data parmas:@{93 @ "username": @ "999", 94 @ "type": @ "XML"}];95}96 @end
Additional notes:
File upload request data format
MimeType of some files
iOS development-Uploading of Web files