zoukankan      html  css  js  c++  java
  • asp.net中大文件下载

    因为IIS支持的最大文件为int32的最大值位数的文件下载,所以,超过2G的文件无法通过IIS进行下载。

    通过网上查找的资料,如下可实现文件的下载,使用filestream进行下载。

            public void download()
            {
                System.IO.Stream iStream = null;
    
                byte[] buffer = new Byte[10000];//设置大小
                int length;
                long dataToRead;
                string filepath = Server.MapPath("~/Upload/XXX/YYY.rar");//文件的路径
                string filename = System.IO.Path.GetFileName(filepath);
    
                try
                {
                    iStream = new System.IO.FileStream(filepath, FileMode.Open, FileAccess.Read);
                    dataToRead = iStream.Length;
    
                    Response.Clear();
                    Response.ClearHeaders();
                    Response.ClearContent();
                    Response.ContentType = "application/unknow"; 
                    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);
                            Response.Flush();
    
                            buffer = new Byte[10000];
                            dataToRead = dataToRead - length;
                        }
                        else
                        {
                            dataToRead = -1;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Response.Write("Error : " + ex.Message);
                }
                finally
                {
                    if (iStream != null)
                    {
                        iStream.Close();
                    }
    
                    Response.End();
                }
            }  
  • 相关阅读:
    【FZYZOJ】细菌 题解(最短路)
    oracle 开发 第02章 查询
    oracle 开发 第01章 简介
    rhel配置163、epel、rpmforge的yum源
    linux 手动清除缓存
    查看linux内核版本和发行版本
    linux下安装rlwrap
    linux下安装vncserver
    Nagios 安装
    linux mutt发送报表
  • 原文地址:https://www.cnblogs.com/syp1Blog/p/10899576.html
Copyright © 2011-2022 走看看