Multi-File upload for iOS development

Source: Internet
Author: User

//

Viewcontroller.m

b03- multiple file uploads

//

Created by 0426iOS on 15/7/1.

Copyright (c) year 0426iOS. All rights reserved.

//

#import "ViewController.h"

@interface viewcontroller ()

@end

@implementation Viewcontroller

-(void) viewdidload {

[Super viewdidload];

additional setup after loading the view, typically from a nib.

}

-(void) Touchesbegan: (nsset *) touches withevent: (uievent *) event{

nsstring *strurl = @ "http://127.0.0.1/upload/upload-m.php";

// load two files

nsstring *filename1 = @ "oo.png";

nsstring *path1 = [[nsbundle mainbundle] pathforresource: fileName1 ofType: Nil];

nsdata *data1 = [nsdata datawithcontentsoffile:p ath1];

nsstring *filename2 = @ "xx.png";

nsstring *path2 = [[nsbundle mainbundle] pathforresource: fileName2 OfType :nil];

nsdata *data2 = [nsdata datawithcontentsoffile:p ath2];

nsdictionary *files = @{filename1:data1,filename2:data2};

Nsdictionary *params = @{@ "username":@ "Hhhhhhhhhhh"};

// upload multiple files

[self uploadfiles:strurl files:files name:@ "userfile[]" params:params];

}

#define Kboundary @"AABBCC"

Uploading multiple Files

-(void) Uploadfiles: (NSString *) strurl files: (nsdictionary *) files Name: (NSString *) name params: ( Nsdictionary *) param{

Nsurl *url = [Nsurl Urlwithstring:strurl];

Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];

Request. HttpMethod = @ "POST";

//content-type:multipart/form-data, boundary=----webkitformboundarycwujbgnwchuhgprt

NSString *ct = [NSString stringWithFormat:@ "multipart/form-data; boundary=%@", kboundary];

[Request SETVALUE:CT Forhttpheaderfield:@ "Content-type"];

//

Request. Httpbody = [self makebody:files name:name Params:param];

[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response, NSData *data, Nserror *connectionerror) {

//

if (!connectionerror) {

Nshttpurlresponse *httpresponse = (Nshttpurlresponse *) response;

if (httpresponse.statuscode = =) {

//

ID json = [nsjsonserialization jsonobjectwithdata:data options:0 Error:NULL];

NSLog (@ "%@", JSON);

}Else{

NSLog (@ " Server Internal Error ");

}

}Else{

NSLog (@ " request error %@", connectionerror);

}

}];

}

//

-(NSData *) Makebody: (Nsdictionary *) files Name: (NSString *) name params: (nsdictionary *) param{

Nsmutabledata *mdata = [Nsmutabledata data];

// Prepare documents

[Files enumeratekeysandobjectsusingblock:^ (NSString *key, NSData *data, BOOL *stop) {

------WEBKITFORMBOUNDARYCWUJBGNWCHUHGPRT

Content-disposition:form-data; Name= "userfile[]"; Filename= "Snip20150701_10.png"

Content-type:image/png

//1

nsmutablestring *mstr = [nsmutablestring string];

[MSTR AppendFormat:@ "\r\n--%@\r\n", kboundary];

[MSTR AppendFormat:@ "content-disposition:form-data; name=\"%@\ "; filename=\"%@\ "\ r \ n", Name,key];

[MSTR appendString:@ "application/octet-stream\r\n"];

[MSTR appendString:@ "\ r \ n"];

[Mdata appenddata:[mstr datausingencoding:nsutf8stringencoding];

//2

[Mdata Appenddata:data];

}];

// prepare data in the form

[Param enumeratekeysandobjectsusingblock:^ (NSString *key, NSString *value, BOOL *stop) {

------WEBKITFORMBOUNDARYCWUJBGNWCHUHGPRT

Content-disposition:form-data; Name= "username"

//

345345

nsmutablestring *mstr = [nsmutablestring string];

[MSTR AppendFormat:@ "\r\n--%@\r\n", kboundary];

[MSTR AppendFormat:@ "content-disposition:form-data; name=\"%@\ "\ r \ n", key];

[MSTR appendString:@ "\ r \ n"];

[MSTR AppendFormat:@ "%@\r\n", value];

[Mdata appenddata:[mstr datausingencoding:nsutf8stringencoding];

}];

NSString *end = [NSString stringWithFormat:@ "--%@--\r\n", kboundary];

[Mdata appenddata:[end datausingencoding:nsutf8stringencoding];

return mdata.copy;

}

@end

Multi-File upload for iOS development

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.