zoukankan      html  css  js  c++  java
  • 文件流下载方法

    /// <summary>
            /// 用流的方式下载文件
            /// </summary>
            /// <param name="filepath_name">文件路径及文件名</param>
            public static void FileDownLoad(string filepath_name)
            {
                System.IO.Stream iStream = null;
    
                //以10K为单位缓存:
                byte[] buffer = new Byte[10000];
    
                int length;
    
                long dataToRead;
    
                // 制定文件路径.
                string filepath = filepath_name;
    
                //  得到文件名.
                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 = iStream.Length;
    
                    System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
                    System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
    
                    while (dataToRead > 0)
                    {
                        //保证客户端连接
                        if (System.Web.HttpContext.Current.Response.IsClientConnected)
                        {
                            length = iStream.Read(buffer, 0, 10000);
    
                            System.Web.HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
    
                            System.Web.HttpContext.Current.Response.Flush();
    
                            buffer = new Byte[10000];
                            dataToRead = dataToRead - length;
                        }
                        else
                        {
                            //结束循环
                            dataToRead = -1;
                        }
                    }
                }
                catch (Exception ex)
                {
                    // 出错.
                    System.Web.HttpContext.Current.Response.Write("Error : " + ex.Message);
                }
                finally
                {
                    if (iStream != null)
                    {
                        //关闭文件
                        iStream.Close();
                    }
                }
            }
  • 相关阅读:
    OCP-1Z0-053-V12.02-622题
    OCP-1Z0-053-V12.02-501题
    Flex实现查询和重置
    闪回事务处理回退
    闪回数据归档测试
    闪回数据归档
    OCP-1Z0-053-V12.02-166题
    VC-摄像头控制SDK源码
    MFC 的 Picture Control 加载 BMP/PNG 图片的方法
    OCP-1Z0-051-V9.02-25题
  • 原文地址:https://www.cnblogs.com/binlyzhuo/p/9923484.html
Copyright © 2011-2022 走看看