Download and open a file in a separate window

  1. #1

    Download and open a file in a separate window

    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.

  2. #2

    RE: Download and open a file in a separate window

    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";
  3. #3

    RE: Download and open a file in a separate window

    Hi,

    yes, i'm currently using IE.


    I will try to change the content type.


    Thank You.

Similar Threads

  1. [CLOSED] Moving javascript into a separate .js file
    By ewgoforth in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Feb 04, 2011, 8:10 PM
  2. [CLOSED] [1.0] File download in MVC
    By tdracz in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Nov 23, 2009, 4:43 PM
  3. Download file in MVC project
    By danni in forum 1.x Help
    Replies: 0
    Last Post: Aug 10, 2009, 7:50 AM
  4. Replies: 2
    Last Post: Dec 26, 2008, 11:29 AM
  5. File download
    By speedstepmem4 in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Dec 05, 2008, 7:22 AM

Posting Permissions