如標題

最近在公司工作時,有同仁遇到這個問題,與另一位同仁協助找答案後,終於是ok囉!

客戶的需求是希望在瀏覽器直接開啟pdf檔,不要再出現詢問下載或直接開啟的視窗!

而我們會在asp.net中用到reporting services所做的報表,轉成pdf供客戶下載。

因為程式碼的部份需要稍微改寫..

以下是實作後的c# code,xxx表示取得資料;

假設我有一個download.aspx的網頁,內容如下,記得這一頁不要有iframe哦,不然會沒有效果

     private void Page_Load(object sender, System.EventArgs e)
     {
       DataSet ds=xxxx;

   if (ds.Tables[0].Rows.Count>0)
   {
    Response.Write(ds.Tables[0].Rows[0]["x"].ToString());
    string newName=HttpUtility.UrlEncode(ds.Tables[0].Rows[0]["x"].ToString(),System.Text.Encoding.UTF8);
    Response.Clear();
    
//以下為原先的code
    //Response.AddHeader("Content-Disposition","attachment; filename=" + newName);
    /Response.ContentType="application/octet-stream";

    //以下為變更後的code
    Response.AddHeader("Content-Disposition","inline; filename=" + newName);
    Response.ContentType="Application/PDF";

    Response.AddHeader("Content-Length", (ds.Tables[0].Rows[0]["y"] as byte[]).Length.ToString());
    Response.BinaryWrite(ds.Tables[0].Rows[0]["y"] as byte[]);
    Response.End();
   }

}

以下是呈現的結果..,為保護內容,所以有做了一些效果保護pdf的內容

2009-4-15 下午 05-29-45.jpg 

arrow
arrow
    全站熱搜

    丫德 發表在 痞客邦 留言(1) 人氣()