close

protected void LinkButton5_Click(object sender, EventArgs e)
    {
        string filename = "xxx.pdf";
        //從Server端取得檔案 
        Stream FileStream;
        FileStream = File.OpenRead(Server.MapPath("~/xxx/xxx.pdf"));
        downloadBook(filename, FileStream);
    }
    protected void downloadBook(string filename, Stream FileStream)
    {
        Byte[] Buf = new byte[FileStream.Length];
        FileStream.Read(Buf, 0, int.Parse(FileStream.Length.ToString()));
        FileStream.Close();

        //準備下載檔案 
        Response.ClearHeaders();
        Response.Clear();
        Response.Expires = 0;
        Response.Buffer = false;
        Response.ContentType = "Application/save-as";
        Response.Charset = "utf-8";
        //透過Header設定檔名 
        Response.AddHeader("Content-Disposition", "Attachment; filename=" + HttpUtility.UrlEncode(filename));
        Response.BinaryWrite(Buf);
        Response.End();
    }

arrow
arrow
    文章標籤
    C# 直接下載檔案
    全站熱搜
    創作者介紹
    創作者 amychang2014 的頭像
    amychang2014

    工作需要筆記

    amychang2014 發表在 痞客邦 留言(0) 人氣()