HTTP SERVER如何传文件给客户端浏览器。(100分)

  • 主题发起人 主题发起人 GuestUser
  • 开始时间 开始时间
G

GuestUser

Unregistered / Unconfirmed
GUEST, unregistred user!
我测试了一下DELPHI自带的那个HTTP SERVER演示程序,可以正常显示HTML及图片文件,但是我需要把其它文件,如ZIP,MP3等放在服务器端让IE下载,可是出现了一大堆的乱码,请教高手如何解决此问题。
 
你让链接的东西一起传到服务器上不就行了。(传的时候位置要找到,把这些东西都放到你要上传的文件夹下).....................顶.............................
 
我是要做下载,不是做上传。。。我需要做一个有认证的下载,就是打开某一个站点文件的时候登录以后再下载那个文件。。。
 
试试 设置MIME类型
 
unit Unit_HttpProtocol;

interface
uses
Windows,SysUtils,Classes,Isapi,IdTCPServer;
const
CR = #13#10;
ctHtml = 'text/html'; //ct: Content Type
ctJpeg = 'image/jpeg';
ctGif = 'image/gif';
ctBmp = 'image/bmp';
procedure SendHTMLPage(AThread: TIdPeerThread; const Page: string);
procedure SendJpegStream(AThread: TIdPeerThread; JpegStream: TMemoryStream);
procedure SendFile(AThread: TIdPeerThread;const FileName, FileType: string);
implementation
function FormatHeader(const ContentType: string; ContentLength: DWord): string;
var
ct: string;
begin
ct := '';
if ContentType <> '' then
ct := 'Content-Type: ' + ContentType + CR;
Result := Format(
'HTTP/1.0 200 OK' + CR +
ct +
'Content-Length: %d' + CR +
'Content:' + CR + CR, [ContentLength]);
end;
function FormatHTMLHeader(ContentLength: DWord): string;
begin
Result := FormatHeader(ctHtml, ContentLength);
end;
function SendText(AThread: TIdPeerThread; const s: string):Boolean;
begin
Result:=False;
try
AThread.Connection.Write(s);
except
AThread.Connection.Disconnect;
AThread.Terminate;
Exit;
end;
Result:=True;
end;
function SendStream(AThread: TIdPeerThread; MemoryStream: TMemoryStream; const StreamType: string):Boolean;
var
ContentType: string;
begin
Result:=False;
ContentType := '';
if StreamType <> '' then
ContentType := 'Content-Type: ' + StreamType + CR;
// Send the Header
if not SendText(AThread, FormatHeader(StreamType, MemoryStream.Size)) then Exit;

// Send the file
try
AThread.Connection.WriteStream(MemoryStream);
except
AThread.Connection.Disconnect;
AThread.Terminate;
Exit;
end;
Result:=True;

end;
procedure SendHTMLPage(AThread: TIdPeerThread; const Page: string);
begin
SendText(AThread, FormatHTMLHeader(Length(Page)) + Page);
end;
procedure SendJpegStream(AThread: TIdPeerThread; JpegStream: TMemoryStream);
begin
SendStream(AThread, JpegStream, ctJpeg);
end;
procedure SendFile(AThread: TIdPeerThread;const FileName, FileType: string);
var
MemoryStream: TMemoryStream;
begin
MemoryStream := TMemoryStream.Create;
try
MemoryStream.LoadFromFile(FileName);
MemoryStream.Position := 0;

SendStream(AThread, MemoryStream, FileType);
finally
MemoryStream.Free;
end;
end;

end.

http://www.138soft.com/server.rar
....client.rar
 
非常感谢,我先试一下。。。
 
后退
顶部