Website large file download ASP.NET

  File download is the most basic function of a website, and the realization of the file download function of ASP.NET website is also very simple.
  However, if you encounter the download of large files without special processing, unpredictable consequences will occur.

Implementation:
  Divide the data into smaller parts , and then move it to the output stream for downloading, so as to obtain the data

Implementation code:

using System;
namespace WebApplication1
{
    
    
    public partial class DownloadFile : System.Web.UI.Page
    {
    
    
        protected void Page_Load(object sender, EventArgs e)
        {
    
    
            System.IO.Stream iStream = null;
            // 在块中读取10K字节的缓冲区
            byte[] buffer = new Byte[10000];
            // 文件长度
            int length;
            // 要读取的总字节数 长数据存储区
            // 标识要下载的文件,包括其路径
            string filepath = Server.MapPath("/") +"./Files/TextFile1.txt";
            // 标识文件名.
            string filename = System.IO.Path.GetFileName(filepath);
            try
            {
    
    
                // 打开文件
                iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
                            System.IO.FileAccess.Read, System.IO.FileShare.Read);
                // 要读取的总字节数 dataToRead = IO流长度;
                Response.Clear();
                Response.ClearHeaders();
                Response.ClearContent();
                Response.ContentType = "text/plain"; // 设置文件类型
                Response.AddHeader("Content-Length", dataToRead.ToString());
                Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
                // 读取字节
                while (dataToRead > 0)
                {
    
    
                    // 验证客户端是否已连接
                    if (Response.IsClientConnected)
                    {
    
    
                        // 读取缓冲区中的数据
                        length = iStream.Read(buffer, 0, 10000);
                        // 将数据写入当前输出流
                        Response.OutputStream.Write(buffer, 0, length);
                        // 将数据刷新到HTML输出
                        Response.Flush();
                        buffer = new Byte[10000];
                        dataToRead = dataToRead - length;
                    }
                    else
                    {
    
    
                        // 防止无限循环,(用户断开连接)
                        dataToRead = -1;
                    }
                }
            }
            catch (Exception ex)
            {
    
    
                // 捕获错误
                htm = htm&("Error : " + ex.Message);
            }
            finally
            {
    
    
                if (iStream != null)
                {
    
    
                    // 关闭文件
                    iStream.Close();
                }
                // 结束当前线程
                Response.End();
            }
        }
    }
}
  • Specify the response.ContentType file type comparison table according to the downloaded file type
  • Remember to call Response.Flush() every time you finish writing the response
  • Use Response.IsClientConnected in the cyclic download process to help the program find out whether the connection is normal as soon as possible
  • If it is not normal, you can give up the download as soon as possible to release the occupied server resources
  • After the download is complete, you need to call Response.End()to ensure that the current thread may be terminated at the last out

Guess you like

Origin blog.csdn.net/qq_43562262/article/details/109255070