1 //2 //VIEWCONTROLLER.M3 //ios_0206_ File Upload4 //5 //Created by Ma C on 16/2/6.6 //copyright©2016 Blog Technology. All rights reserved.7 //8 9 #import "ViewController.h"Ten #import "SSZipArchive.h" One A #defineBwfileboundary @ "----------Bowenkeji" - #defineBwnewline @ "\ r \ n" - #defineBwencode (str) [STR datausingencoding:nsutf8stringencoding] the - @interfaceViewcontroller () - - @end + - @implementationViewcontroller + A- (void) Viewdidload { at [Super Viewdidload]; -Self.view.backgroundColor =[Uicolor Cyancolor]; - } - -- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event - { in [self createzip]; - } to ///Compress Files +- (void) Createzip - { theNSString *caches =[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; * //0. Folders that need to be compressed $NSString *images = [caches stringbyappendingpathcomponent:@"Images"];Panax Notoginseng //1. Create a ZIP file -NSString *zipfile = [caches stringbyappendingpathcomponent:@"Images.zip"]; theBOOL result =[ssziparchive createzipfileatpath:zipfile withcontentsofdirectory:images]; + A if(Result) { theNSString *mimetype =[self mimetype:[nsurl fileurlwithpath:zipfile]]; +NSData *data =[NSData Datawithcontentsoffile:zipfile]; -[Self Upload:@"Images.zip"andmimetype:mimetype andfiledata:data Andparams:nil]; $ } $ - } - ///Unzip the file the- (void) UnZip - {WuyiNsurl *url = [Nsurl urlwithstring:@"Http://localhost:8080/MJServer/resources/images.zip"]; theNsurlsessiondownloadtask *task = [[nsurlsession sharedsession] Downloadtaskwithurl:url completionHandler:^ (NSURL * _ Nullable location, Nsurlresponse * _nullable response, Nserror *_nullable Error) { - WuNSString *caches =[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; - [ssziparchive unzipFileAtPath:location.path todestination:caches Uniqueid:nil]; About $ }]; - [Task resume]; - } - A ///MimeType of the file +-(NSString *) MIMEType: (Nsurl *) URL the { - //1. Create a request $Nsurlrequest *request =[Nsurlrequest Requestwithurl:url]; the theNsurlresponse *response =Nil; the //2. Send request (return response) the[Nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:nil]; - //3. Get MimeType in returnResponse. MIMEType; the } the About ///File Upload Package the- (void) Upload: (NSString *) filename andmimetype: (NSString *) MimeType andfiledata: (NSData *) FileData theAndparams: (Nsdictionary *) Dict the { + //1. Request Path -Nsurl *url = [Nsurl urlwithstring:@"Http://localhost:8080/MJServer/upload"]; the //2. Create a POST requestBayiNsmutableurlrequest *request =[Nsmutableurlrequest Requestwithurl:url]; theRequest. HttpMethod =@"POST"; the //2. Set the request header (tell the server to upload the file data this time) -NSString *contenttype = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", bwfileboundary]; -[Request Setvalue:contenttype Forhttpheaderfield:@"Content-type"]; the //3. Set the request body theNsmutabledata *body =[Nsmutabledata data]; the the //4.1 File Parameters -[Body Appenddata:bwencode (@"--")]; the [Body Appenddata:bwencode (bwfileboundary)]; the [Body Appenddata:bwencode (Bwnewline)]; theNSString *disposition = [NSString stringWithFormat:@"content-disposition:form-data; name=\"File\"; filename=\ "%@\"", filename];94 [Body appenddata:bwencode (disposition)]; the [Body Appenddata:bwencode (Bwnewline)]; the theNSString *type = [NSString stringWithFormat:@"Content-type:%@", MimeType];98 [Body Appenddata:bwencode (type)]; About [Body Appenddata:bwencode (Bwnewline)]; - 101 //Specific Content102 [Body Appenddata:bwencode (Bwnewline)];103 [Body appenddata:filedata];104 [Body Appenddata:bwencode (Bwnewline)]; the 106 //4.2 Non-file parameters (user name parameter)107 108[Dict enumeratekeysandobjectsusingblock:^ (ID_nonnull Key,ID_nonnull obj, BOOL *_nonnull stop) {109[Body Appenddata:bwencode (@"--")]; the [Body Appenddata:bwencode (bwfileboundary)];111 [Body Appenddata:bwencode (Bwnewline)]; the 113NSString *disposition = [NSString stringWithFormat:@"content-disposition:form-data; name=\"%@\"", key]; the the [Body appenddata:bwencode (disposition)]; the [Body Appenddata:bwencode (Bwnewline)];117 118 [Body Appenddata:bwencode (Bwnewline)];119 [Body appenddata:bwencode ([obj description]); - [Body Appenddata:bwencode (Bwnewline)];121 122 }];123 //4.3 end tag124[Body Appenddata:bwencode (@"--")]; the [Body Appenddata:bwencode (bwfileboundary)];126[Body Appenddata:bwencode (@"--")];127 [Body Appenddata:bwencode (Bwnewline)]; - 129Request. Httpbody =body; the 131 //5. Sending the request the[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse * _nullable response, NSData * _nullable data, Nserror *_nullable Connectionerror) {133 134Nsdictionary *dict =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves Error:nil];135NSLog (@"%@", dict);136 }];137 }138 139 @end
ios-Network (file compression and decompression)