iOS uploads files via post

Source: Internet
Author: User

because iOS cannot upload images through an HTML form, to upload images, you must implement an HTTP request, rather than uploading it via the post of an HTML form, as in other languages.

The format of the HTTP POST request for uploading images is this:

Java code
  1. Content-type:multipart/form-data, boundary=aab03x
  2. --aab03x
  3. Content-disposition:form-data; Name="Field1"
  4. Hello boris!
  5. --aab03x
  6. Content-disposition:form-data; Name="pic"; Filename="Boris.png"
  7. Content-type:image/png
  8. ... contents of boris.png ...
  9. --aab03x--


The first line specifies that the HTTP POST request is encoded as multipart/form-data (this must be used for uploading the file).
Boundary=aab03x explained that aab03x was the dividing line. Like--aab03x is the meaning of a dividing line.

content-disposition:form-data; name= "Field1"

Hello boris!

This statement declares the name of a field in the request, such as field1 and the value of the field, such as Hello boris!
This is similar to the <input name= "field1" type= "text" value= "Hello boris!" in form form />
A blank line in the middle is required.

Separate fields are separated by dividing lines, which require a separate line, such as--aab03x--

The next line of demarcation is the next field

content-disposition:form-data; name= "pic"; filename= "Boris.png"
Content-type:image/png

... contents of boris.png ...
--aab03x--

Here we declare the variable pic, which is the file we want to pass, and we need to specify file Name:filename= "Boris.png" when uploading files.
and you need to specify the format of the file on the following line: Content-type:image/png


... contents of boris.png ... Here is boris.png binary content, such as <89504e47 0d0a1a0a 0000000d 49484452 000000b4 000000b4 08020000 00b2af91 65000020 00494441 547801 2c dd79b724 6b7616f6 8c888c88 8c9c8733 55ddb1d5 6a0db486 06218401 ...

at the end of the HTTP POST request, there needs to be a dividing line, but there are both before and after:--aab03x--

The above formats are HTTP specifications, each blank line, and spaces are required.



below is the implementation code for iOS:
#define Http_content_boundary @ "Wanpush"-(BOOL) Httpputdata: (nsstring*) strURL FilePath: (nsstring*) FilePath DataType :(nsstring*) dataType {    strURL = [strURL stringbyaddingpercentescapesusingencoding:nsutf8stringencoding] ;    nsurl* url = [nsurl urlwithstring:strurl];        nsdata* data = [NSData datawithc ontentsoffile:filepath];    nsstring* fileName = [FilePath lastpathcomponent];        nsstring* strbodybegin = [NSString stringwithformat:@ "--%@\ncontent-disposition:form-data; name=\"%@\ "; Filename=\ "%@\" \ncontent-type:%@\n\n ", http_content_boundary, @" file ",  filename, datatype];    nsstring* strbodyend = [NSString stringwithformat:@ "\n--%@--",http_content_boundary];        Nsmutabledata *httpbody = [Nsmutabledata data];    [Httpbody appenddata:[strbodybegin datausingencoding:nsutf8stringencoding]];    [Httpbody appenddata:data];   [Httpbody appenddata:[strbodyend datausingencoding:nsutf8stringencoding]];        nsmutableurlrequest* httpputrequest = [[Nsmutableurlrequest alloc] init];    [httpputrequest SetURL:url];     [httpputrequest sethttpmethod:@ "POST"];    [httpputrequest settimeoutinterval:60000];  & nbsp [Httpputrequest setvalue:[nsstring stringwithformat:@ "%@", @ (httpbody.length)] forhttpheaderfield:@ "Content-Length "];    [httpputrequest setvalue:[nsstring stringwithformat:@" multipart/form-data; boundary=%@ ", HTTP_ Content_boundary] forhttpheaderfield:@ "Content-type"];    httpputrequest.httpbody = httpBody;         nshttpurlresponse* HttpResponse = nil;    Nserror *error = [[Nserror alloc] init];  &nb Sp NSData *responsedata = [nsurlconnection sendsynchronousrequest:urlrequest returningresponse:&httpresponse Error: &error];    if (HttpResponse = nil) {        NSLog (@ "URL:%@\nerror_code:%@", strURL, error);        return no;   }  &nbsp ; if (httpresponse.statuscode! =) {        NSLog (@ "URL:%@\nhttp response:%ld", strURL, (long) HttpR Esponse.statuscode);        return no;   }        return YES;

Background PHP Code:
<?php    $target _path  = "./tmp/";//      $target _path = $target _path. $_files[' file ' [' name ']);    $target _path = Iconv ("UTF-8", "gb2312", $target _path);    if (move_uploaded_file ($_files[' file ' [' Tmp_name '], $target _path)) {         echo "the file". $_files[' file ' [' Name ']). "has been uploaded.";    } else{         echo "There is an error uploading the file, please try Again! Error Code: ". $_files[' file ' [' name '];    }? >



iOS uploads files via post

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.