Delphi Can

Orjinalini görmek için tıklayınız: Web Server Uygulaması Sadece html publish ediyor
Şu anda (Arşiv) modunu görüntülemektesiniz. Orjinal Sürümü Görüntüle internal link
Projemde Delphi7 ile yazmış olduğum programda 80 nolu portu açarak IDHTTPServer ile bir index sayfası yayınlamak istiyorum
Fakat Localhosttan Browser ile baktığımda html görüntüleniyor sayfa yükleniyor ama resimler yüklenmiyor
bunun izahı nedir? İternette araştırdığım kodların hepsinde aynı durum.

Formun üzerinde bir adet IDHTTPServer var
active , port 80
edit1.text := 'www'; //klasörü
edit2.text:= 'index.html' //sayfası
edit3.text:= ''; //Boş

procedure TForm1.IdHTTPServer1CommandGet(AThread: TIdPeerThread;
 ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
//INDEX
begin
if (ARequestInfo.Document='') or (ARequestInfo.Document='/') then begin
AResponseInfo.ContentType:='text/html';
IdHTTPServer1.ServeFile(AThread, AResponseinfo,'www\index.html') end else
//ERROR 404
if (ARequestInfo.Document<>'') or (ARequestInfo.Document<>'/') then
if fileexists(Edit2.Text+'\'+ARequestInfo.Document)=false then
IdHTTPServer1.ServeFile(AThread, AResponseinfo,Edit2.Text+'\'+Edit3.Text) else
//CONTENT TYPE
begin
if Pos('.GIF',ANSIUPPERCASE(ARequestInfo.Document))>0 then AResponseInfo.ContentType:='image/gif'
else if Pos('.JPG',ANSIUPPERCASE(ARequestInfo.Document))>0 then AResponseInfo.ContentType:='image/jpg'
else if Pos('.JPEG',ANSIUPPERCASE(ARequestInfo.Document))>0 then AResponseInfo.ContentType:='image/jpeg'
else if Pos('.BMP',ANSIUPPERCASE(ARequestInfo.Document))>0 then AResponseInfo.ContentType:='image/bmp'
else if Pos('.PNG',ANSIUPPERCASE(ARequestInfo.Document))>0 then AResponseInfo.ContentType:='image/png'
else if (Pos('.HTML',ANSIUPPERCASE(ARequestInfo.Document))>0) or
(Pos('.HTM',ANSIUPPERCASE(ARequestInfo.Document))>0) or
(Pos('.PHP',ANSIUPPERCASE(ARequestInfo.Document))>0) or
(Pos('.CSS',ANSIUPPERCASE(ARequestInfo.Document))>0) or
(Pos('.TXT',ANSIUPPERCASE(ARequestInfo.Document))>0) or
(Pos('.XML',ANSIUPPERCASE(ARequestInfo.Document))>0) or
(Pos('.JS',ANSIUPPERCASE(ARequestInfo.Document))>0) then AResponseInfo.ContentType:='text/html'
else AResponseInfo.ContentType:='application/octet-stream';
IdHTTPServer1.ServeFile(AThread, AResponseinfo,Edit2.Text+'\'+ARequestInfo.Document);
end;  END;  



c:\proje\www\index.html dosyası ve resimler (*.jpg) projeyle aynı yerde
IdHTTPServer1.ServeFile(AThread, AResponseinfo,Edit2.Text+'\'+ARequestInfo.Document); ifadesinde URL üzerinden talep edilen dosya 'ya bilgisayarınız üzerinde de aynı şekilde erişmeye çalışmışsınız.

IdHTTPServer1.ServeFile(AThread, AResponseinfo,'www\index.html') gibi ifadelerle dosyalarınızı istemci ye sunarken mutlaka bilgisayarınızdaki tam yolu kullanmalısınız.

Dosya ekinde kaynak kodlarınız üzerinde ufak revizeler yaptım. Herşey yolunda giderse istediğiniz gibi çalışacağını umuyorum.
Bu Şekilde yapınca çalıştı, WebServerdeki Root Dizini = REQUESTFILES adında string oluyor
IDHttpServer bileşeni rootu alıp içindekileri publish edebilmek için, pathin sonunda ARequestInfo.Document sözdiziminin olması gerekiyormuş.
edit3ü de gözden kaçırmışım meğerse boş path ve  dediğiniz gibi tam yolu belirtmediğim için çalışmadı

ROOT = REQUESTFILES = örn : c:\proje\www\
Edit1.text:= 'www';
Edit2.text:= ''index.html';

procedure TForm1.IdHTTPServer1CommandGet(AThread: TIdPeerThread;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
REQUESTFILES:STRING; // ROOT FOLDER
BEGIN
try
REQUESTFILES:=ExtractFilePath(ParamStr(0))+'\'+edit1.Text+'\'; // ROOT FOLDER www yada http gibi
if (ARequestInfo.Document='') or (ARequestInfo.Document='/') then begin
AResponseInfo.ContentType:='text/html';
IdHTTPServer1.ServeFile(AThread, AResponseinfo,REQUESTFILES+Edit2.Text) end else
//ERROR 404
if (ARequestInfo.Document<>'') or (ARequestInfo.Document<>'/') then
if fileexists(REQUESTFILES+ARequestInfo.Document)=false then
IdHTTPServer1.ServeFile(AThread, AResponseinfo,REQUESTFILES+Edit2.Text) // burayı da 404 not found html yapayım index yok ise
 else
//CONTENT TYPE
begin
if Pos('.GIF',ANSIUPPERCASE(ARequestInfo.Document))>0 then AResponseInfo.ContentType:='image/gif'
else if Pos('.JPG',ANSIUPPERCASE(ARequestInfo.Document))>0 then AResponseInfo.ContentType:='image/jpg'
else if Pos('.JPEG',ANSIUPPERCASE(ARequestInfo.Document))>0 then AResponseInfo.ContentType:='image/jpeg'
else if Pos('.BMP',ANSIUPPERCASE(ARequestInfo.Document))>0 then AResponseInfo.ContentType:='image/bmp'
else if Pos('.PNG',ANSIUPPERCASE(ARequestInfo.Document))>0 then AResponseInfo.ContentType:='image/png'
else if (Pos('.HTML',ANSIUPPERCASE(ARequestInfo.Document))>0) or
(Pos('.HTM',ANSIUPPERCASE(ARequestInfo.Document))>0) or
(Pos('.PHP',ANSIUPPERCASE(ARequestInfo.Document))>0) or
(Pos('.CSS',ANSIUPPERCASE(ARequestInfo.Document))>0) or
(Pos('.TXT',ANSIUPPERCASE(ARequestInfo.Document))>0) or
(Pos('.XML',ANSIUPPERCASE(ARequestInfo.Document))>0) or
(Pos('.JS',ANSIUPPERCASE(ARequestInfo.Document))>0) then AResponseInfo.ContentType:='text/html'
else AResponseInfo.ContentType:='application/octet-stream';
IdHTTPServer1.ServeFile(AThread, AResponseinfo,REQUESTFILES+ARequestInfo.Document);
except
end;
END;


Şöyle bir çözüm daha buldum, bu daha sade
procedure TForm1.IdHTTPServer1CommandGet(AThread: TIdPeerThread;
 ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
 LocalDoc: string;
 ByteSent: Cardinal;
 ResultFile: TFileStream;
begin
TRY
begin
   LocalDoc := ExpandFilename(ExtractFilepath(Application.ExeName)+'\RAPORLAR\www\' + ARequestInfo.Document);
   if not FileExists(LocalDoc) and DirectoryExists(LocalDoc) and FileExists(ExpandFileName(LocalDoc + '/index.html')) then
   begin
     LocalDoc := ExpandFileName(LocalDoc + '/index.html');  // Buraya 404 not found html ekleyeyim
   end;
   if FileExists(LocalDoc) then
   begin
       if AnsiSameText(ARequestInfo.Command, 'HEAD') then
       begin
         ResultFile := TFileStream.create(LocalDoc, fmOpenRead or fmShareDenyWrite);
         try
           AResponseInfo.ResponseNo := 200;
           AResponseInfo.ContentType := 'text/html';
           AResponseInfo.ContentLength := ResultFile.Size;
         finally
           ResultFile.Free;
         end;
       end
       else
       begin
         ByteSent := IDHTTPServer1.ServeFile(AThread, AResponseInfo, LocalDoc);
        end;
   end
   else 
   begin
     AResponseInfo.ResponseNo := 404;
     AResponseInfo.ContentText := '<html><head><title> Error (HATA) </title></head><body><h1>' + AResponseInfo.ResponseText + '</h1></body></html>';
   end;
 end;
EXCEPT
MEMO2.Lines.Insert(0,'HATA OLUŞTU');
END;
 Image1.Picture.LoadFromFile(extractfilepath(Application.ExeName)+'\RAPORLAR\www\test.jpg');
// statusbar1.Panels.Items[3].Text:=DOSYABOYUTU(extractfilepath(Application.ExeName)+'\RAPORLAR\www\test.jpg');

END;

Ufak bir ekleme yapmanın size performans anlamında yardımcı olacağını düşünüyorum.
IDHTTPServer1.ServeFile(AThread, AResponseInfo, LocalDoc); ifadesinden aşağıdaki gibi kod satırı eklerseniz, istemci tarayıcısı daha önce indirdiği dosya için bir önbellek kontrolü yapar ve dosya tarihinde değişme olmadıysa önbellek ten kullanmaya devam eder. Bu, her seferinde sunucunuzdan yeni dosya indirilmemesini sağlar.
AResponseInfo.LastModified :=

    DateTimeToStr(FileDateToDateTime(FileAge(LocalDoc)));