利用 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