- Write File Upload class UploadFile.h
// // UploadFile.h// 02.Post upload //// Created by Wyh on 15-1-29.// Copyright (c) 2015 Itcast. All rights reserved. // #import <Foundation/Foundation.h>@interface uploadfile:nsobject-( void) Uploadfilewithurl: (nsurl *) URL data: (NSData *) data; @end
- Write File Upload class UPLOADFILE.M
////UPLOADFILE.M//02.Post Upload////Created on 15-1-29.//Copyright (c) 2015 Itcast. All rights reserved.//#import "UploadFile.h"@implementationUploadFile//Stitching StringsStaticNSString *boundarystr =@"--";//Delimited StringStaticNSString *randomidstr;//this upload flag stringStaticNSString *uploadid;//Upload (PHP) script, receive file fields-(instancetype) init{ Self=[Super Init]; if(self) {randomidstr=@"Itcast"; Uploadid=@"UploadFile"; } returnSelf ;}#pragmaMark-Private Method-(NSString *) Topstringwithmimetype: (NSString *) MimeType uploadfile: (NSString *) uploadfile{nsmutablestring*STRM = [nsmutablestringstring]; [StrM AppendFormat:@"%@%@\n", Boundarystr, Randomidstr]; [StrM AppendFormat:@"content-disposition:form-data; name=\"%@\"; filename=\ "%@\" \ n", Uploadid, UploadFile]; [StrM AppendFormat:@"Content-type:%@\n\n", MimeType]; NSLog (@"%@", StrM); return[StrM copy];}-(NSString *) bottomstring{nsmutablestring*STRM = [nsmutablestringstring]; [StrM AppendFormat:@"%@%@\n", Boundarystr, Randomidstr]; [StrM appendString:@"content-disposition:form-data; name=\"Submit\"\ n"]; [StrM appendString:@"submit\n"]; [StrM AppendFormat:@"%@%@--\n", Boundarystr, Randomidstr]; NSLog (@"%@", StrM); return[StrM copy];}#pragmaMark-Upload file-(void) Uploadfilewithurl: (nsurl *) URL data: (NSData *) data{//1> Data BodyNSString *topstr = [Self Topstringwithmimetype:@"Image/png"UploadFile:@"Avatar 1.png"]; NSString*bottomstr =[self bottomstring]; Nsmutabledata*datam =[Nsmutabledata data]; [Datam appenddata:[topstr datausingencoding:nsutf8stringencoding]; [Datam Appenddata:data]; [Datam appenddata:[bottomstr datausingencoding:nsutf8stringencoding]; //1. RequestNsmutableurlrequest *request = [Nsmutableurlrequest requestwithurl:url cachepolicy:0timeOutInterval:2.0f]; //Datam is released when the scope is out, so no copyRequest. Httpbody =Datam; //2> Setting the header property of the requestRequest. HttpMethod =@"POST"; //3> Setting Content-lengthNSString *strlength = [NSString stringWithFormat:@"%ld", (Long) Datam.length]; [Request Setvalue:strlength Forhttpheaderfield:@"Content-length"]; //4> Setting Content-typeNSString *strcontenttype = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", Randomidstr]; [Request Setvalue:strcontenttype Forhttpheaderfield:@"Content-type"]; //3> Connection Server send request[Nsurlconnection sendasynchronousrequest:request Queue:[[nsoperationqueue alloc] init] completionhandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {NSString*result =[[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; NSLog (@"%@", result); }];}@end
- Controller call
////MJVIEWCONTROLLER.M//02.Post Upload////Created by Wyh on 15-1-29.//Copyright (c) 2015 Itcast. All rights reserved.//#import "ViewController.h"#import "UploadFile.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload{[Super Viewdidload]; UploadFile*upload =[[UploadFile alloc] init]; NSString*urlstring =@"http://localhost/upload.php"; NSString*path = [[NSBundle mainbundle] Pathforresource:@"Avatar 1.png"Oftype:nil]; NSData*data =[NSData Datawithcontentsoffile:path]; [Upload Uploadfilewithurl:[nsurl urlwithstring:urlstring] data:data];}@end
iOS development-File Upload principle