iOS elearning------10 Native API file uploads

Source: Internet
Author: User

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 the URL is the http://localhost/~apple/itcast/upload.php//Data body parameter 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, set the method [Request sethttpmethod:@ "POST"];    2) Set the data body//1> set boundary string, you can reuse 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, you need to specify Content-type and content-length nsstring *contentstr = [NSString stringwithformat:@] when uploading a file 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 because the user is required to confirm that the file was uploaded successfully. When uploading files using HTTP, there is usually a size limit.    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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.