ios通過post上傳檔案

來源:互聯網
上載者:User

ios通過post上傳檔案
由於iOS無法通過html表單來上傳圖片,因此想要上傳圖片,必須實現http請求,而不能像其他語言那樣通過html表單的post就能上傳。

上傳圖片的http post請求的格式是這樣的:

Java代碼

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

    第一行是指定了http post請求的編碼方式為multipart/form-data(上傳檔案必須用這個)。
    boundary=AaB03x說明了AaB03x為分界線。比如 --AaB03x 就是一個分界線的意思

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

    Hello Boris!

    這句話聲明了請求中的一個欄位的名稱,如field1 以及欄位的值,如Hello Boris!
    這裡類似form表單中的
    中間的空行是必須的。

    不同的欄位之間用分界線分開,分界線需要單獨一行,如 --AaB03x--

    分界線的下一行,是下一個欄位

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

    ... contents of boris.png ...
    --AaB03x--

    這裡聲明了變數pic,也就是我們要傳的檔案,上傳檔案的時候需要在後邊指定file name:filename="boris.png"
    並且需要在下一行指定檔案的格式:Content-Type: image/png


    ... contents of boris.png ... 這裡是boris.png的二進位內容,如 <89504e47 0d0a1a0a 0000000d 49484452 000000b4 000000b4 08020000 00b2af91 65000020 00494441 5478012c dd79b724 6b7616f6 8c888c88 8c9c8733 55ddb1d5 6a0db486 06218401 ......

    在http post請求的結尾,需要有一個分界線,但是是前後都有--的:--AaB03x--

    以上的這些格式,是http的規範,每個空行,空格都是必須的。



    下邊是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 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;}

    後台php代碼:



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.