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>");
                }
            }
  • 相关阅读:
    Yii2的深入学习--行为Behavior
    使用WordPress搭建自己的博客
    php-resque的设计和使用
    PHP的学习--在Atom中使用XDebug(Mac)
    七牛镜像的使用
    macOS平台下虚拟摄像头的研发总结
    macOS下利用dSYM文件将crash文件中的内存地址转换为可读符号
    XCode日常使用备忘录
    DirectShow Filter的开发实践
    Windows下程序启动时出现0xc000007b错误的解决方案
  • 原文地址:https://www.cnblogs.com/ingvner/p/7703832.html
Copyright © 2011-2022 走看看