ajax實現文件下載


前台:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="../../Lib/jquery.js"></script>
    <script>
        $(document).ready(function ()
        {
            $("#Button1").click(function ()
            {
                location.href = 'Web7.ashx'
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Button1" type="button" value="下載" />
    </div>
    </form>
</body>
</html>

 

 

 

 

一般處理程序

        string s_fileName = "交代事項需求.docx";
            HttpContext.Current.Response.ContentType = "application/ms-download";
            string s_path = HttpContext.Current.Server.MapPath( s_fileName ) ;
            System.IO.FileInfo file = new System.IO.FileInfo( s_path );
            HttpContext.Current.Response.Clear( );
            HttpContext.Current.Response.AddHeader( "Content-Type" , "application/octet-stream" );
            HttpContext.Current.Response.Charset = "utf-8";
            HttpContext.Current.Response.AddHeader( "Content-Disposition" , "attachment;filename="
          + System.Web.HttpUtility.UrlEncode("abc.docx" , System.Text.Encoding.UTF8 ) );
            HttpContext.Current.Response.AddHeader( "Content-Length" , file.Length.ToString( ) );
            HttpContext.Current.Response.WriteFile( file.FullName );
            HttpContext.Current.Response.Flush( );
            HttpContext.Current.Response.Clear( );
            HttpContext.Current.Response.End( ); 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM