码迷,mamicode.com
首页 > Web开发 > 详细

ajax实现文件下载

时间:2015-11-12 18:07:39      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

前台:

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

 

ajax实现文件下载

标签:

原文地址:http://www.cnblogs.com/zhanglimei/p/4959667.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!