利用 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();

        }
    }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM