您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 上實作 multipart/form-data 檔案上傳

編輯:iOS 開發綜合

在 iOS 中組出 multipart/form-data 的請求本體

HTTP 請求(以下簡稱報文)由請求標頭與請求主體兩部分組成,格式如下:

 POST / HTTP/1.1 Content-Type:application/x-www-form-urlencoded Accept-Encoding: gzip, deflate Host: w.sohu.com Content-Length: 21 Connection: Keep-Alive Cache-Control: no-cache txt1=hello&txt2=world

要在 iOS 上傳檔案,關鍵在於把 Content-Type 改為 multipart/form-data 並帶上 boundary,然後依分隔符把各個欄位與檔案二進位內容組進 body。Objective-C 的實作如下:

 // #define kHttpRequestHeadContentTypeValueMultipart @"multipart/form-data; boundary=forjoritest" // #define kHttpRequestHeadContentTypeKey @"Content-Type" // #define kHttpRequestHeadBoundaryValue @"forjoritest" // #define kHttpRequestContentDisposition @"Content-Disposition: form-data" NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; NSURL *url = [NSURL URLWithString:[kDetectUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url]; mutableRequest.HTTPMethod = @"POST"; [mutableRequest addValue:kHttpRequestHeadContentTypeValueMultipart forHTTPHeaderField:kHttpRequestHeadContentTypeKey]; NSString *body = [NSString stringWithFormat:@"--%@\r\n%@;name=\"%@\"\r\n\r\n%@", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, kUploadParamApiKey, @"ApiKey"]; body = [body stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n%@; name=\"%@\"\r\n\r\nApiSecret", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, kUploadParamApiSecret]]; body = [body stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n%@; name=\"%@\"\r\n\r\n1", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, @"return_landmark"]]; body = [body stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n%@; name=\"%@\"\r\n\r\ngender,age", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, @"return_attributes"]]; body = [body stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n%@; name=\"%@\"; filename=\"%@\" Content-Type=image/jpeg\r\n\r\n", kHttpRequestHeadBoundaryValue, kHttpRequestContentDisposition, kUploadParamImageFile, kUploadParamImageFile]]; NSMutableData *data = [NSMutableData data]; [data appendData:[body dataUsingEncoding:NSUTF8StringEncoding]]; [data appendData:image]; [data appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", kHttpRequestHeadBoundaryValue] dataUsingEncoding:NSUTF8StringEncoding]]; mutableRequest.HTTPBody = data; [mutableRequest setValue:[NSString stringWithFormat:@"%lu", (unsigned long)data.length] forHTTPHeaderField:@"Content-Length"]; NSURLSessionUploadTask *task = [session uploadTaskWithRequest:mutableRequest fromData:nil completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { }]; [task resume];

幾個實作重點值得留意:boundary 字串在 Content-Type 標頭與 body 分隔符中必須完全一致;每個區塊以 --boundary 起頭,最後以 --boundary-- 收尾;文字欄位與檔案欄位的差別在於後者多了 filename 與自身的 Content-Type;由於 body 混有 UTF-8 文字與二維碼的二進位資料,必須用 NSMutableData 組裝而不能用 NSString;最後別忘了正確設定 Content-Length,否則伺服器可能讀取到不完整的主體。

  1. 上一篇文章: No
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved