• Asp.Net--下载文件


    实现方式1:

        protected void DownLoad_Click(object sender, EventArgs e)
        {
            //获取要下载的文件
            string filename = Server.MapPath("~/upload/计算机科学与技术.rar");
           FileInfo f = new FileInfo(filename);
    
            //设置文件头
            Response.ContentType = "application/zip";
    
            //对文件名进行编码处理,并设置保存时的默认文件名
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(f.Name, System.Text.Encoding.UTF8));
    
            //输出文件
            Response.TransmitFile(filename);
            
        }

    效果:

    image

    实现方式2:

        protected void DownLoad_Click(object sender, EventArgs e)
        {
            //获取要下载的文件
             string file = Server.MapPath("~/upload/DesignPattern.rar");
            FileInfo f = new FileInfo(file);
    
            //清空缓冲区内容
             Response.Clear();
    
            //设置文件头
             Response.AddHeader("Content-Disposition", "attachment;filename="+HttpUtility.UrlDecode(f.Name,System.Text.Encoding.UTF8));
            Response.AddHeader("Content-Length", f.Length.ToString());
            Response.AddHeader("Content-Transfer-Encoding", "binary");
            Response.ContentType = "application/zip";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
    
            Response.WriteFile(f.FullName);
            Response.End();
        }
    下载中文名文件时存在问题.
    效果:
    image 
    实现方式3:
        protected void DownLoad_Click(object sender, EventArgs e)
        {
            //获取要下载的文件,并将数据读入数组
             string filepath = Server.MapPath("~/upload/DesignPattern.rar");
            FileInfo fi = new FileInfo(filepath);
            FileStream fs = new FileStream(filepath, FileMode.Open);
            int filelength = (int)fs.Length;
            byte[] bt = new byte[filelength];
            fs.Read(bt, 0, filelength);
            fs.Close();
    
            //设置文件头及保存时的文件名
             Response.ContentType = "application/zip";
            Response.AddHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlDecode(fi.Name,System.Text.Encoding.UTF8));
            Response.AddHeader("Content-Length", filelength.ToString());
    
            //输出文件
             Response.BinaryWrite(bt);
            Response.Flush();
            Response.End();
        }
    下载中文名文件时存在问题.
    效果:
    image 
    实现方式4:
        protected void DownLoad_Click(object sender, EventArgs e)
        {
            string filepath = Server.MapPath("~/upload/DesignPattern.rar");
            FileStream fs = new FileStream(filepath, FileMode.Open);
            int filelength = (int)fs.Length;
            byte[] b = new byte[filelength];
            fs.Read(b, 0, filelength);
            fs.Close();
    
            Response.ContentType = "application/zip";
            Response.AddHeader("Content-Disposition", "attachment;filename=1.rar");
            Response.AddHeader("Content-Length", filelength.ToString());
    
            Response.OutputStream.Write(b, 0, filelength);
            Response.OutputStream.Close();
            Response.Flush();
            Response.Close();
        }

    实现方式5:

    
    
        protected void DownLoad_Click(object sender, EventArgs e)
        {
            //鑾峰彇瑕佷笅杞界殑鏂囦欢,骞跺皢鏁版嵁璇诲叆鏁扮粍
            string filepath = Server.MapPath("~/upload/aspnetmvc-stepbystep.rar");
            FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
            long filelength = fs.Length;
            int readsize = 102400;
            byte[] b = new byte[readsize];  
    
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment;filename=1.rar");
            Response.AddHeader("Content-Length", filelength.ToString());
    
            while (filelength > 0 && Response.IsClientConnected)
            {
                int receive = fs.Read(b, 0, readsize);
                Response.OutputStream.Write(b, 0, receive);
                Response.Flush();
                b = new byte[readsize];
                filelength = filelength - receive;
            }
            fs.Close();
            Response.OutputStream.Close();
            Response.Close();
        }
     
    注意:
    Response.AppendHeader("content-disposition", "attachment;filename=" + filename);//附件下载
    Response.AppendHeader("content-disposition", "online;filename=" + filename);//在线打开
  • 相关阅读:
    BZOJ 4716 假摔
    【UER #4】量子态的棋盘
    [Lydsy2017省队十连测]最长路径
    [Lydsy2017省队十连测]航海舰队
    [Lydsy2017省队十连测]公路建设
    [Lydsy2017省队十连测]商店购物
    湖南省队集训题【2018】(我也不知道是第几场)
    CXMS 胡策2
    [TJOI2018]异或
    TJOI 2018 数学计算
  • 原文地址:https://www.cnblogs.com/loveYN/p/4509672.html
Copyright © 2020-2023  润新知