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)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 乌拉特中旗| 无极县| 文山县| 邵武市| 仁怀市| 岱山县| 昭通市| 光泽县| 民丰县| 柏乡县| 方城县| 洞口县| 墨玉县| 福安市| 嵊州市| 尤溪县| 朝阳县| 弋阳县| 五台县| 大港区| 谢通门县| 陵水| 措勤县| 民乐县| 古浪县| 剑阁县| 习水县| 平潭县| 香港 | 临湘市| 灵寿县| 上思县| 金寨县| 安多县| 长沙市| 镇宁| 铁岭市| 宜春市| 垫江县| 巩留县| 新巴尔虎左旗|