0

From a file .aspx I need to have a redirect to a webpage, open it then download a file. Following my code:

page Source.aspx

<script runat="server">
      protected override void OnLoad(EventArgs e)
      {
          Response.Redirect("Dest.aspx?download=true");
          base.OnLoad(e);
      }
    </script>

page Dest.aspx

<script runat="server">

    private void Page_Load(object sender, System.EventArgs e)
    {
        string download= (string)Request.QueryString["download"];

        if (download == "true")
        {       
            string url = "myurl/myfile.exe";

            System.Net.HttpWebRequest objRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            System.Net.HttpWebResponse objResponse = (System.Net.HttpWebResponse)objRequest.GetResponse();
            int bufferSize = 1;

            Response.ClearHeaders();
            Response.ClearContent();
            Response.AppendHeader("content-disposition:", @"attachment;filename=""myFileName.exe""");
            Response.AppendHeader("Content-Length", objResponse.ContentLength.ToString());
            Response.ContentType = "application/download";

            byte[] byteBuffer = new byte[bufferSize + 1];
            System.IO.MemoryStream memStrm = new System.IO.MemoryStream(byteBuffer, true);
            System.IO.Stream strm = objRequest.GetResponse().GetResponseStream();
            byte[] bytes = new byte[bufferSize + 1];
            while (strm.Read(byteBuffer, 0, byteBuffer.Length) > 0)
            {
                Response.BinaryWrite(memStrm.ToArray());
                Response.Flush();
            }

            Response.Close();
            Response.End();
            memStrm.Close();
            memStrm.Dispose();
            strm.Dispose();
        }         
    }
</script>

Two problems now:

  1. Calling the page "Source.aspx" the download is automacally started but the page Dest.aspx is not shown in the browser

  2. The downloaded file is called "Dest.aspx" instead of "myFileName.exe" ads I've set with Response.AppendHeader("content-disposition:", @"attachment;filename=""myFileName.exe""");

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
  • Why do you attempt to redirect to a second page you could simply download the file in the first `OnLoad` event. Seems odd to have `script runat="server">` that downloads a file on the user's computer. – Security Hound May 13 '13 at 11:20
  • @Rahul Thanks, solve the second issue. – user2377447 May 13 '13 at 12:12
  • @Ramhound Using OnLoad instead of PageLoad does not solve my first issue. the file has downloaded but the page and its contents are not shown. What I want is to display the page and "then" let the file being downloaded. – user2377447 May 13 '13 at 12:15

2 Answers2

0

For your second problem Issue Try something like that

string attachment = string.Format(CultureInfo.InvariantCulture, "attachment; filename=" + sfilename + "", fi.Name); 
Response.ContentType = "application/octet-stream"; 
Response.AppendHeader("Content-Disposition", attachment);

Hope it works for you.

Rahul
  • 5,603
  • 6
  • 34
  • 57
  • No, only the second issue.. The first problem remain. Is there a way to check if the webpage is completely loaded before to launch the code to download the file? – user2377447 May 13 '13 at 12:42
  • is it neccessary to download the file on page load,,means on `Dest.aspx` page you may use any `button` which says `download file` something like that,,that will download the file and your page will show also. – Rahul May 13 '13 at 12:45
  • means you can also call your download functionality from button click event on `Dest.aspx` – Rahul May 13 '13 at 12:52
  • check that your webpage is load completely you may use `LoadComplete` event of the page just try this http://stackoverflow.com/questions/8587555/code-to-check-page-has-finish-loading-asp-net-c – Rahul May 13 '13 at 13:03
  • I've try to follow the instruction on the link about PageLoad_Complete but the "download file" function continue to start and the page is not loaded, maybe there is something wrong in my code about the downoad? – user2377447 May 13 '13 at 13:14
  • is it neccessary to call download function from page load, you can use an extra button on `Dest.aspx` page and call your download function from it's click event.. – Rahul May 13 '13 at 13:17
0

RIght, what you need to be doing is split the process in two:

  1. Display your page and set a redirect value in the header to automatically redirect to your download.
  2. download your file.

the important thing to note is that you can either respond with a web page, OR with a file, not both at once.

Captain Kenpachi
  • 6,960
  • 7
  • 47
  • 68