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;
                  }
              }
          }

          posted on 2009-04-02 18:34 sanmao 閱讀(188) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 博爱县| 巧家县| 历史| 长沙县| 循化| 曲沃县| 梅州市| 元谋县| 嵊泗县| 墨竹工卡县| 屏边| 蕉岭县| 河源市| 博爱县| 深圳市| 郸城县| 宁安市| 凤阳县| 阳信县| 湖口县| 泗阳县| 准格尔旗| 莱芜市| 务川| 买车| 镇平县| 城口县| 法库县| 根河市| 南通市| 民丰县| 金乡县| 喜德县| 台北县| 临安市| 自治县| 高尔夫| 平遥县| 绍兴市| 胶南市| 横峰县|