最近在学习jQuery,使用jQuery的ajax和JSON可以较好地在ASP.NET网站前端和服务器端无刷新网页交换信息。因为网站应用系统中经常要下载Excel报表等文件,就在网上查询了无刷新下载文件的方法,该方法的关键之处有两点:
- 在客户端创建一个临时隐藏的iframe元素
- 将ashx文件的url赋给Iframe的src属性
以下为客户端html文件,其中http://localhost/json为.net 2.0网站的虚拟路径。
<html>
<head>
<script type="text/javascript">
function download()
{
DownloadFileByIframe("http://localhost/json/download.ashx");
}
function DownloadFileByIframe(url)
{
var iframe = document.getElementById("TempCreatedIframeElement");
if(iframe == null)
{
iframe = document.createElement("iframe");
iframe.id = "TempCreatedIframeElement";
}
iframe.style