Einzelnen Beitrag anzeigen

Schokohase
(Gast)

n/a Beiträge
 
#6

AW: TIdHTTPServer umd Umlaute

  Alt 13. Sep 2019, 13:41
Und hier ein bisserl Quelltext
Delphi-Quellcode:
type
  TForm1 = class(TForm)
    IdHTTPServer1: TIdHTTPServer;
    CheckBox1: TCheckBox;
    ActionList1: TActionList;
    Action1: TAction;
    procedure Action1Execute(Sender: TObject);
    procedure Action1Update(Sender: TObject);
    procedure IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
  private
    { Private-Deklarationen }
    procedure AddTemplateFile(const AFilename: string; var s: string);
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Action1Execute(Sender: TObject);
begin
  IdHTTPServer1.Active := not IdHTTPServer1.Active;
end;

procedure TForm1.Action1Update(Sender: TObject);
begin
  TAction(Sender).Checked := IdHTTPServer1.Active;
end;

procedure TForm1.AddTemplateFile(const AFilename: string; var s: string);
begin
  // hier wird es spannend
  // die Dateien sind UTF8-kodiert, also geben wir das mit an
  s := s + TFile.ReadAllText(AFilename,TEncoding.UTF8);

  // Wenn man das so macht, dann gibt es eine komische Anzeige
  //s := s + TFile.ReadAllText(AFilename);
end;

procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  s: string;
begin
  s := '';
  AddTemplateFile('header.txt', s);
  AddTemplateFile('zaehler.txt', s);
  AResponseInfo.ContentType := 'text/html';
  AResponseInfo.ContentEncoding := 'utf-8';
  AResponseInfo.ContentText := s;
end;
Die Dateien (die ich hier zum Testen verwende) header.txt und zaehler.txt auf dem Datenträger sind - bei mir - UTF8 kodiert und genau das muss ich beim Einlesen der Dateien berücksichtigen.

Das was man an AResponseInfo.ContentText übergibt wird von Indy anhand des AResponseInfo.ContentEncoding entsprechend umgewandelt, aber dafür muss man auch etwas Vernünftiges übergeben.
  Mit Zitat antworten Zitat