zoukankan      html  css  js  c++  java
  • 文件下载

    嗯,今天看了会儿ASP.NET文件下载的代码,话不多说,直接上代码

     1      // 上传文件
     2         protected void Button1_Click(object sender, EventArgs e) {
     3 
     4             string path = "~/upload/";
     5 
     6             string FileName = FileUpload1.FileName;
     7 
     8             // 获取后缀名
     9             string Suffix = FileName.Substring(FileName.LastIndexOf('.'));
    10 
    11             // 获取新的文件名
    12             DateTime dateNow = DateTime.Now;
    13             FileName = dateNow.ToString("yyyyMMddHHmmssffff") + Suffix;
    14 
    15             // 获取绝对路径
    16             path = Server.MapPath(path);
    17 
    18             // 检测是否存在此路径,没有就创建
    19             if (!Directory.Exists(path))
    20                 // 创建路径
    21                 Directory.CreateDirectory(path);
    22 
    23             // 拼接文件路径
    24             path += FileName;
    25 
    26             // 上传文件
    27             FileUpload1.SaveAs(path);
    28         }
    29 
    30         // 下载文件
    31         protected void Button2_Click(object sender, EventArgs e) {
    32 
    33             // 默认下载路径
    34             string path = "~/upload/";
    35             path = Server.MapPath(path);
    36 
    37             // 获取指定文件夹
    38             DirectoryInfo dir = new DirectoryInfo(path);
    39             // 若文件夹不存在,返回
    40             if (!dir.Exists) return;
    41 
    42             // 获取指定文件夹下,所有文件
    43             FileInfo[] file = dir.GetFiles();
    44 
    45             // 当指定文件夹下存在文件时,
    46             if (file != null && file.Length > 0) {
    47                 // 取最后一个文件的文件名
    48                 string fileName = file[file.Length - 1].Name;
    49                 // 执行下载方法,返回bool,表示是否下载成功
    50                 bool IsSuccess = DownFile("~/upload/" + fileName);
    51 
    52             }
    53         }
    54 
    55         /// <summary>
    56         /// 下载文件方法
    57         /// </summary>
    58         /// <param name="path">文件的相对路径</param>
    59         public bool DownFile(string path) {
    60 
    61             // 将相对路径转为绝对路径
    62             path = Server.MapPath(path);
    63 
    64             // 根据绝对路径,查找文件
    65             FileInfo file = new FileInfo(path);
    66             // 若文件不存在,返回false
    67             if (!file.Exists)
    68                 return false;
    69 
    70             // 清理各种内容
    71             Response.Clear();
    72             Response.ClearContent();
    73             Response.ClearHeaders();
    74 
    75             // 指定下载后的文件名
    76             Response.AddHeader("Content-Disposition", "attachment;filename=" + file.Name);
    77             // 获取下载的文件大小,会在浏览器中显示
    78             Response.AddHeader("Content-Length", file.Length.ToString());
    79             // 设置文件编码格式,可以为非ASCII 字符,也可以为超长字符(超过1000)
    80             Response.AddHeader("Content-Transfer-Encoding", "binary");
    81             // 设置字符到浏览器的编码,可解决中文乱码,不完全
    82             Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
    83             // 设置可以为任意文件后缀
    84             Response.ContentType = "application/octet-stream";
    85 
    86             // 将文件放入HTTP输出流
    87             Response.WriteFile(file.FullName);
    88 
    89             // 对输出的内容进行缓冲,防止页面卡死
    90             Response.Buffer = true;
    91             // 将缓存的内容输出
    92             Response.Flush();
    93             // 结束输出
    94             Response.End();
    95 
    96             return true;
    97         }
  • 相关阅读:
    Codeforces Round #425 (Div. 2) Problem A Sasha and Sticks (Codeforces 832A)
    bzoj 2301 Problem b
    bzoj 1101 [POI2007]Zap
    bzoj 2005 能量采集
    bzoj 2527 Meteors
    bzoj 2724 [Violet 6]蒲公英
    回顾树状数组
    bzoj 3237 连通图
    bzoj 2733 永无乡
    Codeforces 817C Really Big Numbers
  • 原文地址:https://www.cnblogs.com/Onlooker/p/7353228.html
Copyright © 2011-2022 走看看