流方式上传

  public void FileDownload2()
        {
            string fileName = "新建文件夹2.rar";//客户端保存的文件名 
            string filePath = Server.MapPath("/App_Data/新建文件夹2.rar");//要被下载的文件路径  
            Response.ContentType = "application/octet-stream";//二进制流
            //通知浏览器下载文件而不是打开 
            Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            //以字符流的形式下载文件 
            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                Response.AddHeader("Content-Length", fs.Length.ToString());
                //这里容易内存溢出
                //理论上数组最大长度 int.MaxValue 2147483647
                //(实际分不到这么多,不同的程序能分到值也不同,本人机器,winfrom( 2147483591 相差56)、iis(也差不多2G)、iis Express(只有100多MB))
                byte[] bytes = new byte[(int)fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                Response.BinaryWrite(bytes);
            }
            Response.Flush();
            Response.End();
        }
posted @ 2018-08-07 20:43  Caryon  阅读(133)  评论(0编辑  收藏  举报