zoukankan      html  css  js  c++  java
  • 文件下载的后台代码

    下载事件 
            //下载按钮事件
             protected void lkb_downLoad_Click(object sender, EventArgs e)
             {
                 //获取在隐藏控件的物理路径
                 string fullPath = hf_upPath.Value;
     
                 //获取包含在路径中的文件名
                 string fileName = fullPath.Substring(fullPath.LastIndexOf('\\')+1);
     
                 string extension = System.IO.Path.GetExtension(fullPath);
     
                 //文件操作 
                 FileInfo file = new FileInfo(fullPath);
     
                 //清空缓存区的内容和HTTP头
                 Response.Clear();
                 Response.ClearContent();
                 Response.ClearHeaders();
     
                 //添加HTTP头
                 Response.AddHeader("Content-Disposition", "attachment;filename=" +fileName);
                 Response.AddHeader("Content-Length", file.Length.ToString());
                 Response.AddHeader("Content-Transfer-Encoding", "binary");
     
                 //由于有多种类型文件 判断选择对应的ContentType 
                 switch (extension)
                 {
                     case ".docx":
                     case ".doc":
                         Response.ContentType = "application/msword";
                         break;
                     case ".jpg":
                     case ".jpeg":
                         Response.ContentType = "image/jpeg";
                         break;
                     case ".gif":
                         Response.ContentType = "image/gif";
                         break;
                     case ".txt":
                         Response.ContentType = "text/plain";
                         break;
                     case ".bmp":
                         Response.ContentType = "application/x-bmp";
                         break;
                     default:
                         Response.ContentType = "application/octet-stream";
                         break;
                 }
     
                 //设置响应输出content的内容的编码
                 Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
     
                 //将文件写到输出流
                 Response.WriteFile(file.FullName);
     
                 //向客户端发送当前缓冲区的输出(内容必须大于256字节)
                 Response.Flush();
     
                 //使Web服务器停止处理脚本并返回当前结果
                 Response.End();
             }
  • 相关阅读:
    2019.9.21 Tomcat基于端口的虚拟主机
    shell脚本作业
    DNS原理及其解析过程
    用户管理系统脚本
    pxe批量装机
    磁盘分区挂载脚本
    安装apache脚本
    linux远程拷贝命令及not a regular file 解决方案
    卸载虚拟网卡的方法
    watch的用法
  • 原文地址:https://www.cnblogs.com/lcuzhanglei/p/2613080.html
Copyright © 2011-2022 走看看