TsgcWebSocketServer_HTTPAPI | Send File Response

Use the FileName property of THttpServerResponse object if you want to send a filename as a response to a HTTP request.

 


procedure OnHTTPRequest(aConnection: TsgcWSConnection_HTTPAPI; 
	const aRequestInfo: THttpServerRequest; 
	var aResponseInfo: THttpServerResponse);
begin
  if aRequestInfo.Method = 'GET' then
  begin
    if aRequestInfo.Document = '/test.zip' then
	begin
	  aResponseInfo.ResponseNo := 200;
	  aResponseInfo.FileName := 'c:\download\test.zip';
	  aResponseInfo.ContentType := 'application/zip';
	end
	else
	  aResponseInfo.ResponseNo := 404;
  end
  else 
    aResponseInfo.ResponseNo := 500;
end;

Resumable Downloads

An HTTP 206 Partial Content response is used when a server is fulfilling a request for a specific portion (range) of a resource, instead of sending the entire file. This is commonly used for resumable downloads, media streaming, and large file transfers. 

How it works:

 

Client Requests a Partial Resource: The client (browser, downloader, or media player) sends a Range header specifying the byte range it wants. Example request:

 

GET /video.mp4 HTTP/1.1
Host: example.com
Range: bytes=1000-5000

 

This requests bytes 1000 to 5000 of video.mp4.

 

Server Responds with HTTP 206: If the server supports range requests, it responds with 206 Partial Content and includes a Content-Range header. Example response:

 

HTTP/1.1 206 Partial Content
Content-Range: bytes 1000-5000/1000000
Content-Length: 4001
Content-Type: video/mp4

 

The Content-Range header shows:

 

The range served (1000-5000)

The total size of the file (1000000 bytes).

The Content-Length header is the size of the returned portion (4001 bytes).

 

Client Can Request More Chunks:

The client can send multiple requests for different parts.

This enables resumable downloads and efficient streaming.

 


procedure OnHTTPRequest(aConnection: TsgcWSConnection_HTTPAPI;
const aRequestInfo: THttpServerRequest; var aResponseInfo: THttpServerResponse);
var
  oStream: TFileStream;
  oRanges: TIdEntityRanges;
begin
  oStream := TFileStream.Create('test.pdf', fmOpenRead);
  oRanges := TIdEntityRanges.Create(nil);
  Try
    oRanges.Text := aRequestInfo.Range;
    aResponseInfo.ContentType := 'application/pdf';
    if oRanges.Count > 0 then
    begin
      aResponseInfo.ResponseNo := 206;
      aResponseInfo.AcceptRanges := 'bytes';
      aResponseInfo.ContentRangeStart := oRanges[0].StartPos;
      aResponseInfo.ContentRangeEnd := oRanges[0].EndPos;
      aResponseInfo.ContentRangeInstanceLength := oStream.Size;
      aResponseInfo.ContentStream := TIdHTTPRangeStream.Create(oStream,
        aResponseInfo.ContentRangeStart, aResponseInfo.ContentRangeEnd);
    end
    else
    begin
      aResponseInfo.ResponseNo := 200;
      aResponseInfo.ContentStream := oStream;
    end;
  Finally
    oRanges.Free;
  End;
end;