asp.net下载文件的常用方法

asp.net下载文件的常用方法

//TransmitFile实现下载
    //TransmitFile实现下载
         protected void Button1_Click1(object sender, EventArgs e)
        {
            /*
             微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite
             下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。
             代码如下:
             */
            string strFileName = "三部闲置设备管理系统操作手册IEMS.ppt";
            Response.ContentType = "application/x-zip-compressed";
            //Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            string filename = BLL.Config.PART_EM_UPLOAD_DOC + strFileName; 

            //BLL.Config.PART_EM_UPLOAD_DOC 为路径   ("D:/EMUploadDoc/")
            Response.AddHeader("Content-Disposition", "attachment;filename=" +Server.UrlPathEncode(strFileName));

           //Server.UrlPathEncode()解决文件名的乱码问题.
            
            Response.TransmitFile(filename);
        }
    //WriteFile实现下载
    protected void Button2_Click(object sender, EventArgs e)
    {
        /*
         using System.IO;
        
         */

        string fileName = "asd.txt";//客户端保存的文件名
        string filePath = Server.MapPath("DownLoad/aaa.txt");//路径

        FileInfo fileInfo = new FileInfo(filePath);
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        Response.AddHeader("Content-Transfer-Encoding", "binary");
        Response.ContentType = "application/octet-stream";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.WriteFile(fileInfo.FullName);
        Response.Flush();
        Response.End();
    }

    //WriteFile分块下载
    protected void Button3_Click(object sender, EventArgs e)
    {

        string fileName = "aaa.txt";//客户端保存的文件名
        string filePath = Server.MapPath("DownLoad/aaa.txt");//路径

        System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);

        if (fileInfo.Exists == true)
        {
            const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
            byte[] buffer = new byte[ChunkSize];

            Response.Clear();
            System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
            long dataLengthToRead = iStream.Length;//获取下载的文件总大小
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
            while (dataLengthToRead > 0 && Response.IsClientConnected)
            {
                int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
                Response.OutputStream.Write(buffer, 0, lengthRead);
                Response.Flush();
                dataLengthToRead = dataLengthToRead - lengthRead;
            }
            Response.Close();
        }
    }

    //流方式下载
    protected void Button4_Click(object sender, EventArgs e)
    {
        string fileName = "aaa.txt";//客户端保存的文件名
        string filePath = Server.MapPath("DownLoad/aaa.txt");//路径

        //以字符流的形式下载文件
        FileStream fs = new FileStream(filePath, FileMode.Open);
        byte[] bytes = new byte[(int)fs.Length];
        fs.Read(bytes, 0, bytes.Length);
        fs.Close();
        Response.ContentType = "application/octet-stream";
        //通知浏览器下载文件而不是打开
        Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();

    }

转载于:https://www.cnblogs.com/wj-wangjun/archive/2009/01/01/1366624.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
包含内容部分示例: GridView导出excel 小山的TreeView数据绑定方法 ADO.NET在开发中的部分使用方法和技巧.txt ADO.NET中的视图和过滤器.doc ASP .NET - ArrayList对象.txt asp.net 2.0中TREEVIEW中动态增加结点.txt Asp.net 实现验证码功能的Web控件.txt asp.net常用的javascript经典例子.doc asp.net常用函数表.doc ASP.NET程序中常用的三十三种代码.doc ASP.NET程序中实现校验码图像生成.txt ASP.NET导出EXCEL类.txt ASP.NET导出数据到Excel.txt ASP.NET对IIS中的虚拟目录进行操作.txt asp.net里导出excel表方法汇总.txt ASP.NET文件上传程序的源代码.txt Asp.Net中文本换行.txt ASPNET中实现在线用户检测(使用后台守护线程).txt C# 读取计算机CPU,HDD信息.txt DataGrid导出EXCEL的几个方法(WebControl).txt DataGrid的打印预览和打印.txt GridView排序.txt GRID控件删除之前确认.txt javascript小技巧.doc MVC在Web系统中的模式与应用.doc NET中各种数据库连接大全.doc Treeview控件2.0与ACCESS数据库.txt TreeView使用集锦.txt Web Service服务.doc WEB中实现打印预览.txt 根据用户名生成注册码的算法.txt 关于asp.net导出Excel.txt 汉字转拼音缩写.txt 将DataGrid数据写入Excel文件.txt 揭开ASP.NET中Cookie编程的奥秘.txt 利用TreeView控件动态生成无限级树.txt 实现一个Asp.net自定义Back控件.txt 使用XML创建Excel文档.txt 鼠标停留在GridView某一行时颜色改变.txt 微软提供的加密方法.txt 无法破解的软件注册码算法.txt 用Asp.net实现简单的文字水印.txt 用ASP.Net写一个发送ICQ信息的程序.txt 在ASP.NET中使用Treeview控件和XML.txt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值