马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

ASP.NET下载大文件代码共享

[复制链接]
查看3959 | 回复0 | 2015-1-28 09:46:44 | 显示全部楼层 |阅读模式
此方法可以防止下文件时浏览器崩溃或无响应
代码如下:
[C#] 纯文本查看 复制代码
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;

            // Buffer to read 10K bytes in chunk:
            byte[] buffer = new Byte[10000];

            // Length of the file:
            int length;

            // Total bytes to read.
            long dataToRead;

            // Identify the file to download including its path.
            string filepath = Server.MapPath("/") +"./Files/TextFile1.txt";

            // Identify the file name.
            string filename = System.IO.Path.GetFileName(filepath);

            try
            {
                // Open the file.
                iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
                            System.IO.FileAccess.Read, System.IO.FileShare.Read);

                // Total bytes to read.
                dataToRead = iStream.Length;

                Response.Clear();
                Response.ClearHeaders();
                Response.ClearContent();
                Response.ContentType = "text/plain"; // Set the file type
                Response.AddHeader("Content-Length", dataToRead.ToString());
                Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

                // Read the bytes.
                while (dataToRead > 0)
                {
                    // Verify that the client is connected.
                    if (Response.IsClientConnected)
                    {
                        // Read the data in buffer.
                        length = iStream.Read(buffer, 0, 10000);

                        // Write the data to the current output stream.
                        Response.OutputStream.Write(buffer, 0, length);

                        // Flush the data to the HTML output.
                        Response.Flush();

                        buffer = new Byte[10000];
                        dataToRead = dataToRead - length;
                    }
                    else
                    {
                        // Prevent infinite loop if user disconnects
                        dataToRead = -1;
                    }
                }
            }
            catch (Exception ex)
            {
                // Trap the error, if any.
                Response.Write("Error : " + ex.Message);
            }
            finally
            {
                if (iStream != null)
                {
                    //Close the file.
                    iStream.Close();
                }

                Response.End();
            }
        }
    }
}



1. 将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据。

2. 根据下载的文件类型来指定 Response.ContentType

3. 在每次写完response时记得调用 Response.Flush()

4. 在循环下载的过程中使用 Response.IsClientConnected 这个判断可以帮助程序尽早发现连接是否正常。若不正常,可以及早的放弃下载,以释放所占用的服务器资源。

5. 在下载结束后,需要调用 Response.End() 来保证当前线程可以在最后被终止掉。

C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则