sgcWebSockets のサーバーは、サーバーメモリに影響を与えることなく大きなファイルを POST できます。
HTTP クライアントが multipart/form-data ストリームを送信すると、サーバーはストリームをメモリに保存します。ファイルが大きい場合、サーバーは メモリ不足 の例外が発生する可能性があります。これらの例外を回避するため、サーバーには HTTPUploadFiles というプロパティがあり、POST ストリームの処理方法をメモリ内またはファイルストリームとして設定できます。ストリームをファイルストリームとして処理する場合、受信したストリームはハードディスクに直接保存されるため、メモリの問題が回避されます。
サーバーの設定
multipart/form-data ストリームをファイルストリームとして保存するようにサーバーを設定するには、次の手順に従ってください。
1. プロパティ HTTPUploadFiles.StreamType = pstFileStream を設定します。この設定により、サーバーはこれらのストリームをハードディスクに保存します。
2. ファイルストリームとして保存する 最小バイトサイズ を設定できます。デフォルト値はゼロで、すべてのストリームがファイルストリームとして保存されます。
3. ストリームの保存先フォルダは SaveDirectory で設定します。指定しない場合、アプリケーションと同じフォルダに保存されます。
4. クライアントが multipart/form-data を送信すると、内容は境界(boundary)内にエンコードされています。プロパティ RemoveBoundaries を有効にすると、ストリーム全体の受信後に境界内の内容が自動的に抽出されます。
サンプルコード
// First create a new server instance and set the Streams are saved as File Streams.
oServer := TsgcWebSocketHTTPServer.Create(nil);
oServer.Port := 5555;
oServer.HTTPUploadFiles.StreamType := pstFileStream;
oServer.Active := True;
// Then create a new html file with the following configuration
<html>
<head><title>sgcWebSockets - Upload Big File</title></head>
<body>
<form action="http://127.0.0.1:5555/file" method="post" enctype="multipart/form-data">
<input type="file" name="file_1" />
<input type="submit" />
</form>
</body>
</html>
// Finally open the html file with a web browser and send a file to the server.
// The server will create a new file stream with the extension ".sgc_ps" and when the stream is fully received,
// it will extract the file from the boundaries.
