PDA

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



unaltro2
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"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<ext:ScriptManager ID="Sc1" runat="Server">
</ext:ScriptManager>
<ext:Button runat="server" ID="btnOk" Text="Ok">
<AjaxEvents>
<Click OnEvent="btnOk_Click" IsUpload="True">
</Click>
</AjaxEvents>
</ext:Button>
</form>
</body>
</html>

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.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/pdf";
Response.TransmitFile(file.FullName);
Response.End();
}
}
}

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.

Vladimir
Jan 20, 2010, 3:16 PM
Hi,

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";

unaltro2
Jan 21, 2010, 11:55 AM
Hi,

yes, i'm currently using IE.


I will try to change the content type.


Thank You.