LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#实现文件下载的几种方式

admin
2021年1月29日 9:43 本文热度 1934

先把有问题的代码贴出来吧

 

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.IO;

 

namespace infoPlatClient.NetDisk

{

    public partial class downLoad : Com.DRPENG.Common.WebStruct.BaseForm

    {

 

        /// <summary>

        /// 取得要下载文件的路径

        /// </summary>

        private string fileRpath

        {

            get

            {

                return Request["fileRpath"] == null ? "" : Request["fileRpath"];

            }

        }

        /// <summary>

        /// 取得要下载文件的名称

        /// </summary>

      

        protected void Page_Load(object sender, EventArgs e)

        {

                if (!IsPostBack)

                this.DownloadFile();

        }

        public void DownloadFile()

        {

 

                Response.ClearHeaders();

                Response.Clear();

                Response.Expires = 0;

                Response.Buffer =true;

                Response.AddHeader("Accept-Language", "zh-tw");

                string name = System.IO.Path.GetFileName(fileRpath);

                System.IO.FileStream files = new FileStream(fileRpath, FileMode.Open, FileAccess.Read, FileShare.Read);

                byte[] byteFile=null;

                if (files.Length == 0)

                {

                    byteFile=new byte[1];

                }

                else

                {

                    byteFile = new byte[files.Length];

                }

                files.Read(byteFile, 0, (int)byteFile.Length);

                files.Close();

            

                Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));

                Response.ContentType = "application/octet-stream;charset=gbk";

                Response.BinaryWrite(byteFile);

                Response.End();

             

        }

    }

}

 

 之前一直用这种下载方式,可是有一次用户上传了一个700Mb的文件时报内存溢出的问题,分析了一下原因,用户的内存只有256M,而下载文件时要创建内存流,导致了内存溢出。

 

解决方案:1>WriteFile分块下载,就是每次下载指定数量的多件;

 

             2>通过超链接的方式;

 

             lblDownLoad.Text = "<a href='" + drv["VPath"].ToString() + "'>下载</a>"

 

下面是四种实现文件下载的方式:

 

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.IO;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

 

    //TransmitFile实现下载

    protected void Button1_Click(object sender, EventArgs e)

    {

        /*

         微软为Response对象提供了一个新的方法TransmitFile来解决使用Response.BinaryWrite

         下载超过400mb的文件时导致Aspnet_wp.exe进程回收而无法成功下载的问题。

         代码如下:

         */

 

        Response.ContentType = "application/x-zip-compressed";

        Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");

        string filename = Server.MapPath("DownLoad/z.zip");

        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();

 

    }

}

 


该文章在 2021/1/29 9:47:19 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved