Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an application in which user can download the file. I want to open new window while downloading, I mean when user click on download button, page must be redirect on another page and then after download will start. Just like this code project site, when we try on download any file it'll redirect us on another page and then after download will start. Can any one help me out...?? Thanks in advance....
Posted

Download link points to a new document. On that document you create a javascript-function which location-href's the user to the actual downloadable file.

Download.aspx:
HTML
<body onload="location.href='fileToDownload.zip'">

  <p>Your download will start in a few seconds</p>

</body>


In your case of downloading an image you can add a download.aspx with the following code:

C#
protected void Page_Load(object sender, EventArgs e)
{
    // Get the full pathname for imagefile
    var imageFilename = Request.QueryString["imgName"];
    var path = Server.MapPath("/Images/" + imageFilename)

    // read the file
    using (FileStream fs = File.OpenRead(path))
    {
        int length = (int)fs.Length;
        byte[] buffer;

        using (BinaryReader br = new BinaryReader(fs))
        {
            buffer = br.ReadBytes(length);
        }

        // Force browser to download rather than open the file in browser
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", Path.GetFileName(path)));
        Response.ContentType = "image/jpeg";
        Response.BinaryWrite(buffer);
    }
}


HTML
<body  önload="location.href='download.aspx?imgFile=nicepicture.jpg'">
 
  <p>Your download will start in a few seconds</p>
 
</body>
 
Share this answer
 
v4
Comments
Naikniket 18-Jul-12 9:07am    
I have an image stored in Images folder and now i want to download image stored in images folder.
StianSandberg 18-Jul-12 9:19am    
please see my update answer :)
Naikniket 18-Jul-12 9:48am    
not working... :( :(... i am trying for 3 days but i am not getting it... i need it very urgent... i tried it but same problem.. it downloads the file but not redirects on download page...
hi Naikniket760,

you can use target="_blank" to open in new page and download.

kkakadiya
 
Share this answer
 
Comments
Naikniket 18-Jul-12 9:47am    
i have tried it but in wain.. it's downloads my file but not opens download page...
try this

Response.Redirect into a new window[^]

may be this will help you
thank you
Chetanv
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900