27 November 2008

How to open a pdf Or image file in a new browser window

In the Default.aspx page, in a button click or any other events
----------------------------------------------------------------

ScriptManager.RegisterStartupScript(this, this.GetType(), "s", "window.open('Default2.aspx?FileName=PO0005_R.pdf',null,'height=500, width=500,status= no,resizable= no, scrollbars=no,toolbar=no,menubar=no');", true);

In the Defult2.aspx page
-------------------------

using System.Net;

protected void Page_Load(object sender, EventArgs e)
{
ReadPdfFile(Request.QueryString["FileName"].ToString());
}
private void ReadPdfFile(string FileName)
{
string path = Server.MapPath("PO0005_R.pdf");
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);

if (buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
}

}

No comments: