Reputation: 45947
I have the following code:
protected void Page_Load(object sender, EventArgs e)
{
byte[] buffer = null;
buffer = File.ReadAllBytes("C:\\myfile.pdf");
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.OutputStream.Write(buffer, 0, buffer.Length);
HttpContext.Current.Response.End();
}
I want to open a second window for the pfd file beside the current page, where the pageload comes from.
Upvotes: 2
Views: 24333
Reputation: 6372
In order to do this you'll need to upload the PDF to a path in the application where it can be presented to the user, then register some javascript to open the PDF in a new window:
protected void Page_Load(object sender, EventArgs e)
{
byte[] buffer = null;
buffer = File.ReadAllBytes("C:\\myfile.pdf");
//save file to somewhere on server, for example in a folder called PDFs inside your application's root folder
string newFilepath = Server.MapPath("~/PDFs/uploadedPDF.pdf");
System.IO.FileStream savedPDF = File.Create(newFilepath);
file.Write(buffer, 0, buffer.Length);
file.Close();
//register some javascript to open the new window
Page.ClientScript.RegisterStartupScript(this.GetType(), "OpenPDFScript", "window.open(\"/PDFs/uploadedPDF.pdf\");", true);
}
Upvotes: 1
Reputation: 14308
There is no way to open a new window from a codebehind file. The link to the page on which this Page_Load event is being fired must have the target="_blank"
attribute to open it in a new window. For example:
<a href="DownloadPdf.aspx" target="_blank">Download PDF<a>
On a side note, if this is the only function of your ASPX file, you may want to consider using an HttpHandler instead.
Upvotes: 1
Reputation: 50104
You can't do this from the response.
If you have control of the hyperlink that leads to this page load, you can give it an attrbute target="_blank"
, which will ask the browser to open that link in a new window.
Upvotes: 0