利用 iframe 实现文件 下载

javascript里面      

<script type="text/javascript">

       var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_initializeRequest(downloadfile);
        //触发函数
        function downloadfile(filepath) {
            var iframe = document.createElement("iframe");
            iframe.src = "GenerateFile.aspx?filepath=" + filepath;
            iframe.style.display = "none";
            document.body.appendChild(iframe);
        }
    </script>

      <body>

    <a onclick="downloadfile('filepath') download</a>

  </body>

 

    public partial class GenerateFile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string fileResponse="file content";
            string filepath = Request.QueryString["filepath"];
            Response.AddHeader("Content-disposition", "attachment; filename=report.csv");
            Response.ContentType = "application/octet-stream";
            Response.Write(fileResponse);
            Response.End();

        }
    }

你可能感兴趣的:(iframe)