You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Text;
  8. #region histroy
  9. ///程式代號:OpenFile
  10. ///程式名稱:檔案下載
  11. ///程式說明:
  12. ///xx.YYYY/MM/DD VER AUTHOR COMMENTS(說明修改的內容)
  13. ///01.2012/05/10 1.0 Ethan CREATE
  14. #endregion
  15. namespace OT.Web
  16. {
  17. public partial class OpenFile : System.Web.UI.Page
  18. {
  19. protected void Page_Load(object sender, EventArgs e)
  20. {
  21. // 在這裡放置使用者程式碼以初始化網頁
  22. Response.Expires = 0;
  23. FnOpenFile();
  24. }
  25. private void FnOpenFile()
  26. {
  27. try
  28. {
  29. //防止在IE中直接開啟本程式操作檔案
  30. if (Request["U"] == null && Session["U"] == null)
  31. {
  32. WriteMsg("非法登入,不可操作檔案!");
  33. }
  34. //重定向後開啟檔案
  35. else if (Session["U"] != null)
  36. {
  37. string strFileName = Session["U"].ToString();
  38. Session.Remove("U");//及時釋放Session
  39. this.ReadFile(strFileName);
  40. }
  41. //遠端呼叫本程式時帶入參數檔案名稱U,為防止User看到U的內容,進行一次重定向
  42. else if (Request["U"] != null)
  43. {
  44. string strFileName = Request.QueryString["U"].ToString();
  45. strFileName = FnHidToFileName(strFileName).Replace("\\", "/");
  46. //Session["U"] = strFileName;//用Session記錄U做重定向動作
  47. //Session["FileName"] = Request.QueryString["FileName"].ToString();
  48. //Response.Redirect(@"OpenFile.aspx", false);
  49. this.ReadFile(strFileName);
  50. }
  51. }
  52. catch (Exception ex)
  53. {
  54. Session.Remove("U");//及時釋放Session
  55. this.WriteMsg("開啟檔案出錯,請重新試一次!");
  56. }
  57. }
  58. /// <summary>
  59. /// 將用於URL中傳參的Base64碼轉為String,轉換前將Base64碼中用*和@替換的+和/還原
  60. /// </summary>
  61. /// <param name="strFrom"></param>
  62. /// <returns></returns>
  63. public string FnHidToFileName(string strFrom)
  64. {
  65. string strTo = strFrom.Replace("*", "+").Replace("@", "/").Replace("!", "=");
  66. strTo = FnBase64ToString(strTo);
  67. return strTo;
  68. }
  69. /// <summary>
  70. /// 將Base64碼轉為String
  71. /// </summary>
  72. /// <param name="strFrom"></param>
  73. /// <returns></returns>
  74. public string FnBase64ToString(string strFrom)
  75. {
  76. byte[] byTo = Convert.FromBase64String(strFrom);
  77. string strTo = System.Text.Encoding.Default.GetString(byTo);
  78. return strTo;
  79. }
  80. /// <summary>
  81. /// 開啟檔案
  82. /// </summary>
  83. /// <param name="strFileName"></param>
  84. private void ReadFile(string strFileName)
  85. {
  86. string strDownFileName = UTF_FileName(Request.QueryString["FileName"].ToString());
  87. strDownFileName = HttpUtility.UrlEncode(strDownFileName, System.Text.Encoding.UTF8); //解決中文問題
  88. Response.ContentType = "application/octet-stream";
  89. Response.AddHeader("Content-Disposition"
  90. , String.Format("attachment; filename=\"{0}\";", strDownFileName));
  91. //Response.Write("**" + fileName);
  92. Response.WriteFile(UTF_FileName(strFileName));
  93. try
  94. {
  95. Response.End(); //如果提前結束會出現異常。
  96. }
  97. catch (Exception ex) { return; }
  98. }
  99. private static string UTF_FileName(string filename)
  100. {
  101. return HttpUtility.UrlDecode(filename);
  102. //return HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);
  103. }
  104. /// <summary>
  105. /// Alert訊息並關閉畫面
  106. /// </summary>
  107. /// <param name="Msg">alert出來的訊息內容</param>
  108. private void WriteMsg(string Msg)
  109. {
  110. StringBuilder sbScript = new StringBuilder();
  111. sbScript.Append("<script language='javascript'>");
  112. sbScript.Append("ShowArtAlert('" + Msg + "');");
  113. sbScript.Append("window.opener=null;window.open('','_self');window.close();");
  114. sbScript.Append("</script>");
  115. this.ClientScript.RegisterStartupScript(GetType(), "close", sbScript.ToString());
  116. }
  117. }
  118. }