HTTP Post Big Files

sgcWebSockets Servers allow to Post Big Files without affecting the server memory. 

When a HTTP client sends a multipart/form-data stream, the stream is saved by server in memory. When the files are big, the server can get an out of memory exception, to avoid these exceptions, the server has a property called HTTPUploadFiles where you can configure how the POST streams are handled: in memory or as a file streams. If the streams are handled as file streams, the streams received are stored directly in the hard disk so the memory problems are avoided.

Server Configuration 

To configure your server to save multipart/form-data streams as file streams, follow the next steps:

1. Set the property HTTPUploadFiles.StreamType = pstFileStream. Using this setup, the server will store these streams in the hard disk.

2. You can configure which is the minimum size in bytes where the files will be stored as file stream. By default the value is zero, which means all streams will be stored as file stream.

3. The folder where the streams are stored using SaveDirectory, if not set, will be stored in the same folder where the application is.

4. When a client sends a multipart/form-data, the content is encoded inside boundaries, if the property RemoveBoundaries is enabled, the content of boundaries will be extracted automatically after the full stream is received. 

Sample Code

// 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
    <head><title>sgcWebSockets - Upload Big File</title></head>
        <form action="" method="post" enctype="multipart/form-data">
            <input type="file" name="file_1" />
            <input type="submit" />

// 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.
File Name: sgcHTTPPostBigFile
File Size: 1.8 mb
Download File
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

OAuth2 Client Credentials
openSSL 1.1.1k

Related Posts