View Full Version : Download and open a file in a separate window

Jan 20, 2010, 3:07 PM
Hi everyone, i have to open a pdf file in a separate window. I have tried with this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<form id="form1" runat="server">
<ext:ScriptManager ID="Sc1" runat="Server">
<ext:Button runat="server" ID="btnOk" Text="Ok">
<Click OnEvent="btnOk_Click" IsUpload="True">

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class Default2 : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)


protected void btnOk_Click(object sender, EventArgs e)
FileInfo file = new FileInfo(@"C:\Documents and Settings\koti\Local Settings\Temp\test.pdf");
// Checking if file exists
if (file.Exists)
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/pdf";

but it opens the pdf file in the same and current window.

Anyone of you know how it's possible to reproduce my wished behaviour?

Thank you.

Jan 20, 2010, 3:16 PM

Is it under IE only? I think browser recognize format (using ContentType) and see that it can be opened in the browser window. Try to use another content type

this.Response.ContentType = "application/octet-stream";

or unknown content type

this.Response.ContentType = "text/pdf";

Jan 21, 2010, 11:55 AM

yes, i'm currently using IE.

I will try to change the content type.

Thank You.