http://aawwmate.blog.163.com/blog/static/77528256201092733950315/
indy idhttpserver有关下载的两个问题
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="'+文件名+'"';
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步