iOS development-File Upload principle

Source: Internet
Author: User

    • File Upload
  1. 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

  2. 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

  3. 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

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.