NSMutableURLRequestを使用して画像をWebサーバーにアップロードしています。私のコードは大多数のユーザーに機能するので安定していると思いましたが、一部のユーザーは単に画像をアップロードできません。サーバーログを見ると、たまに見られます
「行0の不明なマルチパート/フォームデータPOSTデータに境界がありません」という警告。これは、ポスト本文の作成における境界エラーを示します。
以下は私が使用しているものです:
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ユーザーに1回発生します。ユーザーがこのエラーを受け取った場合、投稿しようとするすべての画像で同じです。
誰かがすぐに明らかなものを見ることができますか、またはこれが断続的な問題になる理由についての洞察を持っていますか?