Autor Beitrag
Niko S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 566
Erhaltene Danke: 10

Win 7, Ubuntu
Lazarus, Turbo Delphi, Delphu 7 PE
BeitragVerfasst: Mo 28.12.09 16:27 
Hallo Delphianer,
Ich versuch grad ein wenig mit IdHTTPServer zu arbeiten..
nun krieg ich das nicht nicht Grafiken zu senden..
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure TServer.HTTPGetEvent(AContext:TIdContext;
  ARequestInfo: TIdHTTPRequestInfo;
  AResponseInfo: TIdHTTPResponseInfo);
begin
  if (ARequestInfo.Document = '/favicon.ico'then
  begin
    MyStream := TFileStream.Create('.\favicon.ico', fmOpenRead);
    AResponseInfo.ContentStream := MyStream;
    AResponseInfo.ContentType := 'image/x-icon';
  end;
  WriteLn(ARequestInfo.Command);
  WriteLn(ARequestInfo.RemoteIP);
  WriteLn(ARequestInfo.Document);
end;

Nun ich hab mich da nur so rumprobiert aber dieser Ansatz funktioniert nicht warum auch immer...
Er sagt mir nämlich, dass die Grafik fehler enthält.
Weiß da wer mehr als ich?
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mo 28.12.09 16:38 
Das einzige, was mir so direkt auffällt ist, dass du beim Laden des Streams keinen vollständigen Pfad angibst.

Funktioniert es denn mit anderen Dateien? Vielleicht stimmt mit der Grafik ja wirklich was nicht. ;-)

_________________
We are, we were and will not be.
Niko S. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 566
Erhaltene Danke: 10

Win 7, Ubuntu
Lazarus, Turbo Delphi, Delphu 7 PE
BeitragVerfasst: Mo 28.12.09 17:03 
Och menno, es geht das einzige Problem war eig nur der Typ..
image/vnd.microsoft.icon
Ist der richtige type für Icons...
Danke trotzdem =D
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Mo 28.12.09 17:12 
Und trotzdem solltest du dir absolute Dateipfade angewöhnen, sonst hast du früher oder später Probleme

_________________
PROGRAMMER: A device for converting coffee into software.
Niko S. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 566
Erhaltene Danke: 10

Win 7, Ubuntu
Lazarus, Turbo Delphi, Delphu 7 PE
BeitragVerfasst: Mo 28.12.09 17:30 
Ich weiß, ich weiß, dass war jetzt auch nur mehr fürs testen gedacht ^^ Absolute Pfade werden dann gerade bei diesem Projekt mit Variablen festgelegt, irgendwie muss ich dem ja mitteilen wo der die Daten suchen soll =)
matze
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: Di 29.12.09 17:14 
also für sowas gabs (soweit ich das noch weiß) eien Methode, die sich ServeFile() nannte...

EDIT:
Genau das wars: AResponseInfo.ServeFile()

_________________
In the beginning was the word.
And the word was content-type: text/plain.
Niko S. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 566
Erhaltene Danke: 10

Win 7, Ubuntu
Lazarus, Turbo Delphi, Delphu 7 PE
BeitragVerfasst: Di 29.12.09 20:45 
Okay, das wussste ich nicht ich probiers gleich mal aus, so wie ich das jetzt hab, funktioniert es zwar, ist aber doch recht umständlich finde ich und nicht gerade ellegant
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
  ARequestInfo.Document := StringReplace(ARequestInfo.Document, '/''\',[rfReplaceAll]);
  endung := ARequestInfo.Document;
  Delete(endung,1,1);
  ARequestInfo.Document := endung;
  endung := ExtractFileExt(ARequestInfo.Document);
  if not (FileExists(HomeDir+ARequestInfo.Document)) then
  begin
    MyStream := TFileStream.Create(HomeDir+ARequestInfo.Document, fmOpenRead);
    AResponseInfo.ContentStream := MyStream;
    AResponseInfo.ContentType := 'text/html';
  end
  else
  if (endung = '.ico'then
  begin
    MyStream := TFileStream.Create(HomeDir+ARequestInfo.Document, fmOpenRead);
    AResponseInfo.ContentStream := MyStream;
    AResponseInfo.ContentType := 'image/vnd.microsoft.icon';
  end
  else
  if (endung = '.png'then
  begin
    MyStream := TFileStream.Create(HomeDir+ARequestInfo.Document, fmOpenRead);
    AResponseInfo.ContentStream := MyStream;
    AResponseInfo.ContentType := 'image/png';
  end
  else
  if (endung = '.html'or (endung = '.htm'then
  begin
    MyStream := TFileStream.Create(HomeDir+ARequestInfo.Document, fmOpenRead);
    AResponseInfo.ContentStream := MyStream;
    AResponseInfo.ContentType := 'text/html';
  end
  else
  if (endung = '.php'then
  begin
    sl.LoadFromFile(HomeDir+ARequestInfo.Document);
    endung := sl.text;
    While (pos('<?php',endung) > 0Do
      Delete(endung,pos('<?php',endung),pos('?>',endung)-pos('<?php',endung)+2);
    AresponseInfo.ContentText := endung;
    AResponseInfo.ContentType := 'text/html';
  end
  else
  if (endung = '.css'then
  begin
    MyStream := TFileStream.Create(HomeDir+ARequestInfo.Document, fmOpenRead);
    AResponseInfo.ContentStream := MyStream;
    AResponseInfo.ContentType := 'text/css';
  end;
Muck
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 98
Erhaltene Danke: 8

Win 8, Win 7, Vista, Win XP
Delphi XE3, Delphi 2009, Delphi 2007, Delphi 5
BeitragVerfasst: Di 29.12.09 21:02 
Hallo,

hier ein Beispiel (nicht getestet) wie es funktionieren sollte. Ich verwende es ungefaehr so in meinem Server.
GetFileMIMEType setzt den ContentType fuer dich automatisch.
In dem Beispiel wird ResponseNo gesetzt und auch auf HEAD Anfragen reagiert.
Sollte so unter Delphi 2007 laufen, fuer Delphi 2009 musst Du ein paar Anpassungen durchfuehren.
RequestInfo = ARequestInfo etc...

bye

Markus
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
LocalDoc:=HomeDir+RequestInfo.Document;
if fileexists(LocalDoc) then begin
  ResponseInfo.ResponseNo := 200;
  ResponseInfo.ContentType:=HTTPServer.MimeTable.GetFileMIMEType(LocalDoc);
  if AnsiSameText(RequestInfo.Command, 'HEAD'then begin
    // HEAD request, don't send the document but still send back it's size
    ResultFile := TFileStream.create(LocalDoc, fmOpenRead or fmShareDenyWrite);
    try
      ResponseInfo.ContentLength := ResultFile.Size;
    finally
      ResultFile.Free;
    end;
  end else begin
    ResponseInfo.ServeFile(Context,LocalDoc);
  end
end else begin
  ResponseInfo.ContentType:='text/HTML';
  ResponseInfo.ResponseNo := 404// Not found
  ResponseInfo.ContentText := '<html><head><title>Error</title></head><body><h1>' + ResponseInfo.ResponseText + '</h1></body></html>';
end;