Ios uploads files through post

Source: Internet
Author: User

Ios uploads files through post
Because iOS cannot upload images through html forms, to upload images, you must implement http requests instead of using post in html forms in other languages.

The format of the http post request for uploading images is as follows:

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 the encoding method of the http post request as multipart/form-data (this is required for uploading files ).
    Boundary = AaB03x indicates that AaB03x is the demarcation line. For example, "AaB03x" means a dividing line.

    Content-disposition: form-data; name = "field1"

    Hello Boris!

    This statement indicates the name of a field in the request, such as field1 and the value of the field, such as Hello Boris!
    This is similar
    Empty lines in the middle are required.

    Different fields are separated by a line. The line must be a single line, for example, AaB03x --

    The next line of the demarcation line, which is the next field

    Content-disposition: form-data; name = "pic"; filename = "boris.png"
    Content-Type: image/png

    ... Contents of boris.png...
    -- AaB03x --

    The variable pic is declared here, that is, the file to be uploaded. You must specify file name: filename = "boris.png"
    In addition, you must specify the file format: Content-Type: image/png in the next line.


    ... Contents of boris.png... examples of binary content, such as <000000000000000d 49484452 000000b4 000000b4 08020000 00b2af91 65000020 00494441 0000dd79b724 00008c9c8733 55ddb1d5 6a0db000006218401 ......

    At the end of an http post request, a line is required, but both the front and back are --: -- AaB03x --

    The preceding formats are http specifications. Each blank line and space are required.



    The following is the iOS implementation code:
    #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 dataWithContentsOfFile: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];    [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];    NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&httpResponse error:&error];    if (httpResponse == nil) {        NSLog(@"url: %@\nerror_code: %@", strUrl, error);        return NO;    }    if (httpResponse.statusCode != 200) {        NSLog(@"url: %@\nHTTP response: %ld", strUrl, (long)httpResponse.statusCode);        return NO;    }        return YES;}

    Background php code:
       



Related Article

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.