Asp.net大文件下载的实现方法介绍-创新互联

本篇内容介绍了“Asp.net大文件下载的实现方法介绍”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

创新互联主要业务有网站营销策划、成都网站建设、成都做网站、微信公众号开发、成都微信小程序H5高端网站建设、程序开发等业务。一次合作终身朋友,是我们奉行的宗旨;我们不仅仅把客户当客户,还把客户视为我们的合作伙伴,在开展业务的过程中,公司还积累了丰富的行业经验、营销型网站建设资源和合作伙伴关系资源,并逐渐建立起规范的客户服务和保障体系。 

 文件下载是一个网站最基本的功能,ASP.NET网站的文件下载功能实现也很简单,但是如果遇到大文件的下载而不做特殊处理的话,那将会出现不可预料的后果。当我们的网站需要支持下载大文件时,如果不做控制可能会导致用户在访问下载页面时发生无响应,使得浏览器崩溃。可以参考如下代码来避免这个问题。

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", "p_w_upload; 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 。(参考OSChina的这个网址可以找到大部分文件类型的对照表:http://tool.oschina.net/commons)

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

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

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

“Asp.net大文件下载的实现方法介绍”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前标题:Asp.net大文件下载的实现方法介绍-创新互联
网页网址:http://ybzwz.com/article/gceis.html