zoukankan      html  css  js  c++  java
  • 下载文件类代码

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    namespace BaseProject.Classes.Util
    {
        public class DownLoadFile
        {
            public bool Down(string filepath)
            {
                HttpResponse response = HttpContext.Current.Response;
                bool succ = false;
                response.Clear();
                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 = "DownloadFileName";

                // 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.ContentType = "application/octet-stream";
                    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();
                return succ;
            }
        }
    }

  • 相关阅读:
    用PHP判断oicq是否在线的小程序
    我的PHP树的代码,可以嵌套任意层
    用PEAR来写你的下一个php程序(潘凡Night Sailer)(1)
    php在线文本编辑器
    分析HTML,并将结果存到一个数组中。看看里面的注释吧。:)
    聊天室php
    基于文件、数据库的计数器
    hust The mell hell
    UVA 10003 Cutting Sticks(区间DP)
    zoj 3197 Google Book(最小区间覆盖)
  • 原文地址:https://www.cnblogs.com/bestsaler/p/1835707.html
Copyright © 2011-2022 走看看