2004. január 30., péntek

Send binary data from a CGI application


Problem/Question/Abstract:

Set the default file name for saving the data provided as 'response'.

Answer:

It is pretty easy to return any kind of data inside a Delphi CGI Application. But sometimes the data has to be saved under a certain filename, such as "Test.ZIP". To do this you need to add the HTTP header item "Content-Disposition".
To do it in Delphi use the CustomHeaders property. To this TStrings property you can add items in the syntax "name=value" - surprisingly the HTTP syntax name:value is not used here. Example:

procedure TWebModule1.WebModule1CHECKSTATUSAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  s: TFileStream;
begin
  s := nil;
  if request.query = 'download' then
  try
    response.Title := 'Download Test.ZIP';
    response.CustomHeaders.Add('Content-Disposition=filename=Test.zip');
    response.ContentType := 'application/zip';
    s := TFileStream.Create(fmOpenRead + fmShareDenyNone, 'Test.zip');
    response.contentstream := s;
    response.sendresponse;
  finally
    s.Free;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése