iOS multi-image upload

Source: Internet
Author: User
Tags allkeys

began to learn Objc20 many days, recently in a lot of file upload, found some code on the Internet, just a single file, tossing for two days, and finally changed into a multi-file upload.
Xcode 6.1
IOS 8.0 Test Succeeded
I hope other people don't take the time to change as I do. Time is life.
+ (NSString *) Postimagestoserver: (NSString *) strURL dicpostparams: (nsmutabledictionary *) params dicimages: (            Nsmutabledictionary *) dicimages{NSString * RES;    The identifier of the dividing line nsstring *twitterfon_form_boundary = @ "aab03x"; Request//nsmutableurlrequest* request = [Nsmutableurlrequest Requestwithurl:[nsurl Urlwithstring:strurl] based on URL initialization        Cachepolicy:nsurlrequestreloadignoringlocalcachedata Timeoutinterval:10];    Nsurl *url = [Nsurl Urlwithstring:strurl];    Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];    Dividing line--aab03x nsstring *mpboundary=[[nsstring alloc]initwithformat:@ "--%@", twitterfon_form_boundary];    Terminator aab03x--nsstring *endmpboundary=[[nsstring alloc]initwithformat:@ "%@--", mpboundary];    Images to upload UIImage *image;//=[params objectforkey:@ "pic"];    Get the image data//nsdata* data = uiimagepngrepresentation (image);    HTTP body string nsmutablestring *body=[[nsmutablestring alloc]init]; The collection of all keys for the collection of parameters Nsarray *keys= [params allKeys];        Traverse keys for (int i=0;i<[keys count];i++) {//Get current key nsstring *key=[keys objectatindex:i]; If key is not a pic, it indicates that value is a character type, such as Name:boris//if (![            Key isequaltostring:@ "pic"]) {//Add dividing line, newline [body appendformat:@ "%@\r\n", mpboundary];            Add field name, change 2 lines [Body appendformat:@ "content-disposition:form-data; name=\"%@\ "\r\n\r\n", key];            [Body appendstring:@ "content-transfer-encoding:8bit"];        Add the value of the field [body appendformat:@ "%@\r\n", [params objectforkey:key]];        }}////add dividing line, newline//[body appendformat:@ "%@\r\n", mpboundary];    Declaration Myrequestdata, used to put the HTTP body nsmutabledata *myrequestdata=[nsmutabledata data];        Converts the body string to UTF8 format binary [myrequestdata appenddata:[body datausingencoding:nsutf8stringencoding];    Loop add upload image keys = [Dicimages AllKeys]; for (int i = 0; i< [keys Count]; i++) {//picture to upload image = [Dicimages objectforKey:[keys Objectatindex:i]];        Get the image of data nsdata* data = uiimagejpegrepresentation (image, 0.0);        nsmutablestring *imgbody = [[Nsmutablestring alloc] init]; Here loop add picture file//Add picture information Field//Declaration pic field, file name Boris.png//[body appendformat:[nsstring stringwithformat: @ "Co Ntent-disposition:form-data; Name=\ "File\";                Filename=\ "%@\" \ r \ n ", [Keys objectatindex:i]];        Add dividing line, newline [imgbody appendformat:@ "%@\r\n", mpboundary]; [Imgbody appendformat:@ "content-disposition:form-data; name=\" file%d\ "; filename=\"%@.jpg\ "\ r \ n", I, [keys        OBJECTATINDEX:I]];                Declare the format of the uploaded file [imgbody appendformat:@ "Content-type:application/octet-stream; charset=utf-8\r\n\r\n"];                NSLog (@ "uploaded images:%d%@", I, [Keys objectatindex:i]);        Converts the body string to UTF8 format binary//[myrequestdata appenddata:[body datausingencoding:nsutf8stringencoding]]; [Myrequestdata appenddata:[imgbody datausingencoding:nsutf8stringencoding];       Add the data of image to [Myrequestdata Appenddata:data];    [Myrequestdata appenddata:[@ "\ r \ n" datausingencoding:nsutf8stringencoding]];    }//Declaration terminator:--aab03x--nsstring *end=[[nsstring alloc]initwithformat:@ "%@\r\n", endmpboundary];        Join Terminator--aab03x--[Myrequestdata appenddata:[end datausingencoding:nsutf8stringencoding]; Set the value of Content-type in Httpheader nsstring *content=[[nsstring alloc]initwithformat:@ "Multipart/form-data; boundary=%@    ", Twitterfon_form_boundary];    Set httpheader [Request Setvalue:content forhttpheaderfield:@ "Content-type"];    [Request setvalue:@ "keep-alive" forhttpheaderfield:@ "Connection"];    [Request setvalue:@ "UTF-8" forhttpheaderfield:@ "Charsert"]; Set content-length [Request Setvalue:[nsstring stringwithformat:@ "%lu", (unsigned long) [Myrequestdata Length]]    forhttpheaderfield:@ "Content-length"];    Set HTTP body [request Sethttpbody:myrequestdata];        HTTP method [Request sethttpmethod:@ "POST"]; Establish a connection, set proxy//nsurlconnection *conn = [[Nsurlconnection alloc] initwithrequest:request delegate:self];    Set the data NSData *mresponsedata that accepts response;    Nserror *err = nil;        Mresponsedata = [Nsurlconnection sendsynchronousrequest:request returningresponse:nil error:&err];    if (Mresponsedata = = nil) {NSLog (@ "Err Code:%@", [err localizeddescription]);    } res = [[NSString alloc] Initwithdata:mresponsedata encoding:nsutf8stringencoding];        /* IF (conn) {mresponsedata = [nsmutabledata data];                Mresponsedata = [Nsurlconnection sendsynchronousrequest:request returningresponse:nil error:&err];        if (Mresponsedata = = nil) {NSLog (@ "Err Code:%@", [err localizeddescription]);    } res = [[NSString alloc] Initwithdata:mresponsedata encoding:nsutf8stringencoding];    }else{res = [[NSString alloc] init];    }*/NSLog (@ "Server return:%@", res); return res;}


iOS multi-image upload

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.