1

i am writing a code where after clicking one button one page will be redirected to another nd after opening 2nd page one pdf file will be download just like this website http://www.findwhitepapers.com/content22881 .But instead of opening the 2nd page and downloading the pdf file only pdf file is downloaded but 2nd page is not opening.1st page code is

protected void Button1_Click(object sender, EventArgs e)
{
  Response.Redirect("2nd.aspx");
}

2nd page's code is written below.

 protected void Page_Load(object sender, EventArgs e)
{
    string filepath = "guar-gum/Guar-gum-export-report.pdf";

    // The file name used to save the file to the client's system..

    string filename = Path.GetFileName(filepath);
    System.IO.Stream stream = null;
    try
    {
        // Open the file into a stream. 
        stream = new FileStream(Server.MapPath("Guar-gum-export-report.pdf"), System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
        // Total bytes to read: 
        long bytesToRead = stream.Length;
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
        // Read the bytes from the stream in small portions. 
        while (bytesToRead > 0)
        {
            // Make sure the client is still connected. 
            if (Response.IsClientConnected)
            {
                // Read the data into the buffer and write into the 
                // output stream. 
                byte[] buffer = new Byte[10000];
                int length = stream.Read(buffer, 0, 10000);
                Response.OutputStream.Write(buffer, 0, length);
                Response.Flush();
                // We have already read some bytes.. need to read 
                // only the remaining. 
                bytesToRead = bytesToRead - length;
            }
            else
            {
                // Get out of the loop, if user is not connected anymore.. 
                bytesToRead = -1;
            }
        }
        Response.Flush();
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
        // An error occurred.. 
    }
    finally
    {
        if (stream != null)
        {
            stream.Close();




            //
        }
    }
}
SoumitaP
  • 63
  • 2
  • 5
  • 13

2 Answers2

0

What do you expect to see on the 2nd page? All you have there is a pdf file. Do you expect an empty page?

Your redirect works fine. When you click the button a PDF file will be sent back to the browser and it will see it as a file that should be downloaded. No page will be sent to the browser so there is no page to see.

user707727
  • 1,287
  • 7
  • 16
  • please check out http://www.findwhitepapers.com/content22881 page and click download now button.. i want dt same thing in my page also. – SoumitaP Apr 26 '13 at 11:08
0

Here is the solution:

Don't code what you have done in page2.aspx for file downloading instead put an iframe in the page2.aspx and set the src to the file Url.

I guess it is guar-gum/Guar-gum-export-report.pdf in your case. May be you should change this to start from root of the site by prefix / to the file url.

Put this in page2.aspx

<iframe width="1" height="1" frameborder="0" src="[File location]"></iframe>

It is very simple way and No redirects or JavaScript and your Page2.aspx will also open.

UPDATE

Based on the comments below this answer

I think there is no better solution but here is another mindbending one (psst! hope you and other like..) move the page2.aspx code one for file download ONLY to the third page page3.aspx and set iframe.src to page3.aspx

Reference

  1. SO - How to start automatic download of a file in Internet Explorer
Community
  • 1
  • 1
Harsh Baid
  • 7,199
  • 5
  • 48
  • 92
  • What is happening ? Error message or behavior change or else ? – Harsh Baid Apr 26 '13 at 11:41
  • 2nd page is opening but pdf is not downloading. – SoumitaP Apr 26 '13 at 11:45
  • Ok I think there is no better solution but here is another mindbending one (psst hope you and other like).. move the page2.aspx code **one for file download ONLY** to the third page page3.aspx and set `iframe.src` to page3.aspx – Harsh Baid Apr 26 '13 at 11:52
  • this iframe concept is not working... is there any other way to solve the problem? – SoumitaP Apr 26 '13 at 12:10
  • ok [here](http://www.harshbaid.in/wp-content/uploads/2013/04/WebApplication1.gif) is the working example demo site I have created. open in chrome or so and if dwld not happen then press ctrl+s to save the file and rename it to .zip from .gif to see the source. – Harsh Baid Apr 26 '13 at 12:26
  • Works now ? checked the [attachment](http://www.harshbaid.in/wp-content/uploads/2013/04/WebApplication1.gif) ? – Harsh Baid Apr 26 '13 at 12:57
  • autometic download is not happening here also.. is there any problem with my browser.. – SoumitaP Apr 26 '13 at 12:59
  • you mean 1. my [attachment](http://www.harshbaid.in/wp-content/uploads/2013/04/WebApplication1.gif) is not downloading or 2. Your code is not working ? if problem in 1. Then try right-click and 'save link as' or 'Save Target' if problem in 2. then what is current state of problem ? – Harsh Baid Apr 26 '13 at 13:03
  • Can you tell me what is the error message or which part is not working instead of me asking you each time when it is not working.. – Harsh Baid Apr 26 '13 at 13:17
  • Check the `Default.aspx` (which should be page2.aspx in your case) in the `html` and `Contact.aspx` (which should be page3.aspx as I said to send the download file) in the `aspx.cs` code. – Harsh Baid Apr 26 '13 at 13:33