[ASP.NET] 強制下載檔案

.NET 強制下載檔案的三種方式。

public class Utility
{
/// <summary>
    /// 強制下載實體檔案
    /// </summary>
    /// <param name="WebForm">傳Page進來</param>
    /// <param name="FileNameWhenUserDownload">要存檔的檔名(不用完整路徑)。</param>
    /// <param name="FilePath">實體檔案完整路徑+檔名。EX:D:/Download/Files/xxx.zip</param>
    /// <param name="contentType">丟空字串表示用預設:"octet-stream"。下載excel請用"vnd.xls"</param>
    public void DownloadPhysicalFile(System.Web.UI.Page WebForm, string FileNameWhenUserDownload, string FilePath)
    {
        try
        {
            WebForm.Response.ClearHeaders();
            WebForm.Response.Clear();
            WebForm.Response.Expires = 0;
            WebForm.Response.Buffer = true;
            string fileName = FileNameWhenUserDownload;

            if (WebForm.Request.Browser.Browser == "IE")
            {
                fileName = System.Web.HttpUtility.UrlPathEncode(FileNameWhenUserDownload);
            }
            string strContentDisposition = String.Format("{0}; filename=\"{1}\"", "attachment", fileName);

            WebForm.Response.AddHeader("Content-Disposition", strContentDisposition);
            WebForm.Response.ContentType = "Application/octet-stream";
            WebForm.Response.BinaryWrite(System.IO.File.ReadAllBytes(FilePath));
            WebForm.Response.End();
        }
        catch ()
        {
            
        }
    }

    /// <summary>
    /// 強制下載實體檔案
    /// </summary>
    /// <param name="WebForm">傳Page進來</param>
    /// <param name="FileNameWhenUserDownload">要存檔的檔名(不用完整路徑)。</param>
    /// <param name="FilePath">實體檔案完整路徑+檔名。EX:D:/Download/Files/xxx.zip</param>
    public void DownloadPhysicalFile2(System.Web.UI.Page WebForm, string FileNameWhenUserDownload, string FilePath)
    {
        try
        {
            System.Net.WebClient wc = new System.Net.WebClient();
            byte[] a = wc.DownloadData(FilePath);
            wc.Dispose();

            string fileName = FileNameWhenUserDownload;

            if (WebForm.Request.Browser.Browser == "IE")
            {
                fileName = System.Web.HttpUtility.UrlPathEncode(FileNameWhenUserDownload);
            }
            string strContentDisposition = String.Format("{0}; filename=\"{1}\"", "attachment", fileName);

            WebForm.Response.ClearHeaders();
            WebForm.Response.Clear();
            WebForm.Response.Expires = 0;
            WebForm.Response.Buffer = true;
            WebForm.Response.AddHeader("Content-Disposition", strContentDisposition);
            WebForm.Response.ContentType = "Application/octet-stream";
            WebForm.Response.BinaryWrite(a);
            WebForm.Response.End();
        }
        catch ()
        {
           
        }
    }

    /// <summary>
    /// 強制下載實體檔案
    /// </summary>
    /// <param name="WebForm">傳Page進來</param>
    /// <param name="FileNameWhenUserDownload">要存檔的檔名(不用完整路徑)。</param>
    /// <param name="FilePath">實體檔案完整路徑+檔名。EX:D:/Download/Files/xxx.zip</param>
    public void DownloadPhysicalFile3(System.Web.UI.Page WebForm, string FileNameWhenUserDownload, string FilePath)
    {
        try
        {
            FileStream MyFileStream;
            long FileSize;

            MyFileStream = new FileStream(FilePath, FileMode.Open);
            FileSize = MyFileStream.Length;

            byte[] Buffer = new byte[(int)FileSize];
            MyFileStream.Read(Buffer, 0, (int)FileSize);
            MyFileStream.Close();

            string fileName = FileNameWhenUserDownload;

            if (WebForm.Request.Browser.Browser == "IE")
            {
                fileName = System.Web.HttpUtility.UrlPathEncode(FileNameWhenUserDownload);
            }
            string strContentDisposition = String.Format("{0}; filename=\"{1}\"", "attachment", fileName);

            WebForm.Response.ClearHeaders();
            WebForm.Response.Clear();
            WebForm.Response.Expires = 0;
            WebForm.Response.Buffer = true;
            WebForm.Response.AddHeader("Content-Disposition", strContentDisposition);
            WebForm.Response.ContentType = "Application/octet-stream";
            WebForm.Response.BinaryWrite(Buffer);
            WebForm.Response.End();
        }
        catch ()
        {
            
        }
    }

}

留言

這個網誌中的熱門文章

[MS SQL] 使用PIVOT 做兩個欄位的交叉分析(動態欄位)

[ASP.NET] Gridview動態增加欄位與Null值處理

[MS SQL] 將輸入的時間(分、秒)轉換成中文時間的函數