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 閱讀(186) 評論(0)  編輯  收藏

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


          網站導航:
           

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 涞源县| 遂溪县| 江陵县| 临海市| 郯城县| 彰武县| 通化市| 资源县| 台东市| 郎溪县| 承德市| 凤山县| 阳泉市| 静安区| 普宁市| 明星| 达日县| 和龙市| 紫金县| 时尚| 普安县| 黔南| 东光县| 中山市| 延津县| 昆明市| 江川县| 安吉县| 历史| 同心县| 聂荣县| 长垣县| 千阳县| 石首市| 同仁县| 华安县| 伊宁县| 莫力| 上杭县| 浠水县| 桃江县|