我正在使用NSMutableURLRequest將圖像上傳到Web服務器。我認爲我的代碼是穩定的,因爲它適用於大多數用戶,但有些用戶根本無法上傳任何圖像。看着我的服務器日誌,我看到偶爾NSMutableURLRequest偶爾出現「無邊界」錯誤
「中的multipart/form-data發佈的數據缺少邊界在未知在線0」警告這將指向一個邊界錯誤,構建後的身體。
下面是我使用的是什麼:
request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:[method uppercaseString]];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data, boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-type"];
NSMutableData *postBody = [NSMutableData data];
// add the image
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name=\"image_upload\"; filename=\"upload.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSData *jpegData = UIImageJPEGRepresentation(jpeg, 100);
[postBody appendData:jpegData];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// add the other POST params
keys = [postdata allKeys];
int l = [keys count];
for (int i=0; i<l; i++){
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", [keys objectAtIndex:i]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[postdata objectForKey:[keys objectAtIndex:i]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
我可以理解,如果代碼導致了當時的PHP警告100%,但它可能每30個或40個用戶發生一次。如果用戶得到這個錯誤,那麼對於任何他們試圖發佈的圖片來說,它都是一樣的。
任何人都可以立即看到明顯的東西,或有任何見解,爲什麼這將是一個間歇性的問題?