ASP.NET 檔案下載 解決中文亂碼問題

  • 5420
  • 0
  • C#
  • 2013-09-25

ASP.NET 檔案下載 解決中文亂碼問題

<<OOOO.ASPX>>

 

<asp:LinkButton ID="lbnFile1_1" runat="server" OnClick="lbnFile1_1_Click" CssClass="Font_11B">合作特別約定事項</asp:LinkButton>

 

<<OOOO.ASPX.CS>>

 

   protected void Page_Load(object sender, EventArgs e)
    {
        // 使用 UTF8 編碼
        Response.HeaderEncoding = System.Text.Encoding.UTF8; 
        Session.CodePage = 65001;
        Response.Charset = "UFT8";
    }
   #region"下載檔案"
    private void DownFile(string FilePaht, string FileName)
    {
        //To Get the physical Path of the file(test.txt) 
        FileInfo myfile = new FileInfo(FilePaht + FileName);
        // Checking if file exists 
        if (myfile.Exists)
        {
            // Clear the content of the response 
            Response.ClearContent();
            // Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header 
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlPathEncode(FileName));
            // Add the file size into the response header 
            Response.AddHeader("Content-Length", myfile.Length.ToString());
            // Set the ContentType 
            Response.ContentType = ReturnExtension(myfile.Extension.ToLower());
            // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead) 
            Response.TransmitFile(myfile.FullName);
            // End the response 
            Response.End();
        }
    }
 
    private string ReturnExtension(string fileExtension)
    {
        switch (fileExtension)
        {
            case ".htm":
            case ".html":
            case ".log":
                return "text/HTML";
            case ".txt":
                return "text/plain";
            case ".doc":
                return "application/ms-word";
            case ".tiff":
            case ".tif":
                return "image/tiff";
            case ".asf":
                return "video/x-ms-asf";
            case ".avi":
                return "video/avi";
            case ".zip":
                return "application/zip";
            case ".xls":
            case ".csv":
                return "application/vnd.ms-excel";
            case ".gif":
                return "image/gif";
            case ".jpg":
            case "jpeg":
                return "image/jpeg";
            case ".bmp":
                return "image/bmp";
            case ".wav":
                return "audio/wav";
            case ".mp3":
                return "audio/mpeg3";
            case ".mpg":
            case "mpeg":
                return "video/mpeg";
            case ".rtf":
                return "application/rtf";
            case ".asp":
                return "text/asp";
            case ".pdf":
                return "application/pdf";
            case ".fdf":
                return "application/vnd.fdf";
            case ".ppt":
                return "application/mspowerpoint";
            case ".dwg":
                return "image/vnd.dwg";
            case ".msg":
                return "application/msoutlook";
            case ".xml":
            case ".sdxl":
                return "application/xml";
            case ".xdp":
                return "application/vnd.adobe.xdp+xml";
            default:
                return "application/octet-stream";
        }
    }
    #endregion
    protected void lbnFile1_1_Click(object sender, EventArgs e)
    {
        DownFile("C://", "收單合作特別約定事項.doc");
    }