The implementation of uploading files using the original Eco API:
#import "MainViewController.h" @interface Mainviewcontroller () @property (weak, nonatomic) Uiimageview *imageview;@ End@implementation mainviewcontroller-(void) viewdidload{[Super Viewdidload]; Define ImageView and set the image UIImage *image = [UIImage imagenamed:@ "Avatar 1.png"]; Uiimageview *imageview = [[Uiimageview alloc]initwithimage:image]; [ImageView Setframe:cgrectmake (60, 20, 200, 200)]; [Self.view Addsubview:imageview]; Self.imageview = ImageView; UIButton *button = [UIButton buttonwithtype:uibuttontyperoundedrect]; [Button Setframe:cgrectmake (60, 240, 200, 40)]; [Button settitle:@ "upload" forstate:uicontrolstatenormal]; [Self.view Addsubview:button]; [Button addtarget:self action: @selector (Uploadimage) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:button];} #pragma mark upload file (image)-(void) uploadimage{NSLog (@ "upload"); Idea: The Post method that needs to use HTTP to upload the file//call URL is the http://localhost/~apple/itcast/upload.php//data body of the name: file 1 Build url Nsurl *url = [Nsurl urlwithstring:@ "http://localhost/~apple/itcast/upload.php"]; 2 Establish nsmutablerequest nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url]; 1) Set the properties of the request. Setup method [Request sethttpmethod:@ "POST"]; 2) Set the data body//1> set boundary string, can be reused nsstring *boundary = @ "Uploadboundary"; 2> header string Nsmutablestring *startstr = [nsmutablestring string]; [Startstr appendformat:@ "--%@\n", boundary]; [Startstr appendstring:@ "content-disposition:form-data; name=\" file\ "; filename=\" upload.png\ "\ n"]; [Startstr appendstring:@ "content-type:image/png\n\n"]; 3> trailing string nsmutablestring *endstr = [nsmutablestring string]; [Endstr appendformat:@ "--%@\n", boundary]; [Endstr appendstring:@ "content-disposition:form-data:name=\" submit\ "\ n"]; [Endstr appendstring:@ "submit\n"]; [Endstr appendformat:@ "--%@--", boundary]; 4> Mosaic data Body nsmutabledata *bodydata = [NsmutabledaTA data]; [Bodydata appenddata:[startstr datausingencoding:nsutf8stringencoding]; NSData *imagedata = uiimagepngrepresentation (self.imageView.image); [Bodydata Appenddata:imagedata]; [Bodydata appenddata:[endstr datausingencoding:nsutf8stringencoding]; [Request Sethttpbody:bodydata]; 5> specify Content-type, when uploading the file, you need to specify Content-type and content-length nsstring *contentstr = [NSString stringwithformat:@] Multipart/form-data; boundary=%@ ", boundary]; [Request Setvalue:contentstr forkey:@ "Content-type"]; 6> designation content-length Nsinteger length = [bodydata length]; [Request setvalue:[nsstring stringwithformat:@ "%d", length] forkey:@ "content-length"]; 3 Upload the file using the Nsurlconnection synchronization method. The user is required to confirm that the file was uploaded successfully.When uploading files using HTTP, there are generally restricted sizes. Generally not more than 2M. Nsurlresponse *response = nil; Nserror *error = nil; NSData *resultdata = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:& ERROR]; NSString *resultstr = [[NSString alloc]initwithdata:resultdata encoding:nsutf8stringencoding]; NSLog (@ "%@", ResultStr);} @end
iOS elearning------10 Native API file uploads