zoukankan      html  css  js  c++  java
  • 前台页面下载服务器端文件

    点击“下载模板”从服务器端下载excel模板文件。

    按钮点击事件

            /// <summary>
            /// 下载模板按钮点击
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void btnDownloadModel_Click(object sender, EventArgs e)
            {
                DownloadFile(Server.MapPath("~/bin/模版.xls"), "模版.xls");
            }

    方法:Response方法位于System.Web.UI.Page

           /// <summary>
            /// 下载文件
            /// </summary>
            private void DownloadFile(string strPath, string strName)
            {
                try
                {
                    //FileInfo提供创建、复制、删除、移动和打开文件的实例方法    fileInfo变量作为文件路径的包装
                    System.IO.FileInfo fileInfo = new System.IO.FileInfo(strPath);
                    //清除缓冲区中所有内容输出
                    Response.Clear();
                    //设置输出流的HTTP字符集
                    Response.Charset = "GB2312";
                    //设置输出流的HTTP字符集
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    //添加头信息,为“文件下载/另存为”对话框指定默认文件名
                    Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(strName));
                    //添加头信息,指定文件大小,让浏览器能够显示下载信息
                    Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                    //指定返回的是一个不能被客户端读取的流,必须被下载
                    Response.ContentType = "application/ms-excel";
                    //把文件流发送到客户端
                    Response.WriteFile(fileInfo.FullName);
                    //停止页面执行
                    //Response.End();
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                }
                catch (Exception ex)
                {
                    Response.Write("<script>alert('系统出现以下错误://n" + ex.Message + "!//n请尽快与管理员联系.')</script>");
                }
            }
  • 相关阅读:
    zabbix (2.0.6) 历史记录处乱码
    centos 关闭防火墙
    android 带边框的圆角按钮
    Android开发如何在4.0及以上系统中自定义TitleBar
    Android配置时,点击eclipse里Window->Preferences里的android选项出错
    .net 内存泄露检测工具
    UVA 548 Tree
    UVA 536 Tree Recovery
    UVA 514 Rails
    UVA 442 Matrix Chain Multiplication
  • 原文地址:https://www.cnblogs.com/ingvner/p/7703832.html
Copyright © 2011-2022 走看看