[ASP.NET] 下載檔案

  • 3267
  • 0
  • 2008-12-29

[ASP.NET] 下載檔案


        /// <summary>
        /// Download File method
        /// </summary>
        /// <param name="_WebForm">Web Page</param>
        /// <param name="_FilenameForUser">Filename to show</param>
        /// <param name="_FilePath">File Path</param>
        public static void DownloadFile(System.Web.UI.Page _WebForm, String _FilenameForUser, String _FilePath)
        {
            _WebForm.Response.ClearHeaders();
            _WebForm.Response.Clear();
            _WebForm.Response.Expires = 0;
            _WebForm.Response.Buffer = true;
            _WebForm.Response.AddHeader("Accept-Language", "zh-tw");

            //檔案名稱
            _WebForm.Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(_FilenameForUser, System.Text.Encoding.UTF8));
            _WebForm.Response.ContentType = "Application/octet-stream";

            //檔案位置
            _WebForm.Response.WriteFile(_FilePath);
            _WebForm.Response.End();
        }