ios通過post上傳檔案
由於iOS無法通過html表單來上傳圖片,因此想要上傳圖片,必須實現http請求,而不能像其他語言那樣通過html表單的post就能上傳。
上傳圖片的http post請求的格式是這樣的:
Java代碼
- Content-type: multipart/form-data, boundary=AaB03x
-
- --AaB03x
- content-disposition: form-data; name="field1"
-
- Hello Boris!
- --AaB03x
- content-disposition: form-data; name="pic"; filename="boris.png"
- Content-Type: image/png
-
- ... contents of boris.png ...
- --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代碼: