File Download Dialog on Page Load

Discussion in 'ASP .Net' started by ckarbass@gmail.com, Jan 27, 2007.

  1. Guest

    I'd like for the file download dialog to be launched upon page load.
    The workflow or behavior is exactly the same as sourceforge's.

    I've searched on google to no avail. Any help would be appreciated.
    Thanks.
     
    , Jan 27, 2007
    #1
    1. Advertising

  2. Howdy,

    No probs. Different type of content cannot be mixed (well in theory it can,
    anyone interested have a look at this topic:
    http://groups.google.co.uk/group/mi...mixed with html&rnum=3&hl=en#c48b66ab6b35e3db)
    therefore, you must use an iframe inside your main document that will serve
    file as in following example:
    -- main page aspx code --
    <iframe runat="server" id="whatever" src="download.aspx?fileid=123"></iframe>
    -- end --

    -- download.aspx c# code behind --
    public partial class Download : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    // get the file path from query string id
    // be careful what you serve, do not allow
    // any file to be available
    // in my example i added jpeg image copied
    // to the application's root folder
    string filePath = Server.MapPath("~/sunset.jpg");

    System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);

    Response.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Disposition", String.Format("attachment;
    filename=\"{0}\"", filePath));
    Response.AddHeader("Content-Length", fileInfo.Length.ToString());
    Response.WriteFile(filePath);
    Response.End();

    }
    }
    -- end --


    Done.

    Hope this helps

    --
    Milosz


    "" wrote:

    > I'd like for the file download dialog to be launched upon page load.
    > The workflow or behavior is exactly the same as sourceforge's.
    >
    > I've searched on google to no avail. Any help would be appreciated.
    > Thanks.
    >
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Jan 28, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryan Taylor
    Replies:
    2
    Views:
    953
    Ryan Taylor
    Nov 10, 2004
  2. Samy
    Replies:
    2
    Views:
    1,355
  3. Nishi Bhonsle
    Replies:
    0
    Views:
    557
    Nishi Bhonsle
    Feb 25, 2004
  4. Brett  Kelly
    Replies:
    1
    Views:
    684
    Steve C. Orr [MVP, MCSD]
    Jun 16, 2006
  5. Don Peters
    Replies:
    1
    Views:
    114
    Bob Barrows
    Oct 26, 2003
Loading...

Share This Page