1、IdHttpServer返回网页浏览器没直接显示,而是弹出下载对话框的解决方法

最近用Indy10的TIdHttpServer写一个简单的http服务器,原来按网上一个indy9的方法返回服务器目录下的一个html文档,代码如下:

procedure TForm3.IdHTTPServer1CommandGet(AContext: TIdContext;

ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);

const s='D:\MyVcl\Test\idCGIRunner\server\web\index.html';

begin

   //....

AResponseInfo.ServeFile(AContext,s);

//...

end;

结果在在游览器里打开时老出现下载对话框.试了好久,查看生成的http头如下:

Connection: close

Content-Disposition: attachment: filename="index.html";

Content-Type: text/html

Content-Length: 1265

原来它把网页当附件传回来了,再跟Idhttpserver的代码,发现当AResponseInfo.ContentDisposition为空时它会自己加上attachment: filename="index.html";这个参数,经试验,当给AResponseInfo.ContentDisposition随便设一个值结果都是正常的.不过网上查看Content-Disposition的资料说正规的应该有个inline的参数......因此,改成下面就一切OK了:

procedure TForm3.IdHTTPServer1CommandGet(AContext: TIdContext;

ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);

const s='D:\MyVcl\Test\idCGIRunner\server\web\index.html';

begin

AResponseInfo.ContentDisposition:=Format('inline: filename="%s"',[ExtractFileName(s)]);

AResponseInfo.ServeFile(AContext,s);

end;

2、下载文件时,直接从网页打开而没有弹出保存对话框的问题解决

AResponseInfo.CustomHeaders.Values['Content-Disposition'] :='attachment; filename="'+文件名+'"';