views:

986

answers:

4

Hi

I have a aspx page that seems to be loading twice when I enter the Url to the page.

In this page's loading event, I'm making an connection to a server to retrieve a document and then I output the downloaded bytes to the output stream of the page.

This is causing the page to load twice for some strange reason. If I hard code a byte array without making this connection, the page loads once and all is well.

Here are the methods used to retrieve the external document. Maybe you can see something I can't.

public static byte[] GetDocument(string url)
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            Stream stream = myHttpWebResponse.GetResponseStream();

            byte[] _Data = StreamToBytes(stream);

            return _Data;
        }

        private static byte[] StreamToBytes(System.IO.Stream theStream)
        {
            if (theStream == null)
                throw new ArgumentException("URL null.");

            int bytesRead = 0;
            byte[] buffer = new byte[8096];
            MemoryStream bufferStream = new MemoryStream();

            try
            {
                do
                {
                    bytesRead = theStream.Read(buffer, 0, 8096);
                    bufferStream.Write(buffer, 0, bytesRead);

                } while (bytesRead > 0);

            }
            finally
            {
                bufferStream.Flush();
                theStream.Close();
                theStream.Dispose();
            }

            return bufferStream.ToArray();
        }
+1  A: 

These kind of problems often happens due to img tags that have an empty src ...

Thomas Hansen
Yep, I've had this same problem for the same reason.
Ben Daniel
+1  A: 

The likely culprit is having the page directive of AutoEventWireup="true" in addition to OnInit() having this.Page_Load += Page_Load;

Auto Event Wireup does what it sounds like. If there is a method that follows the naming convention, the event is automatically wired up.

You also oftentimes see this on button handlers. The button handler will be set specifically, and the page will also create a button handler if the name follows the convention buttonname_OnClick(sender,args)

Robert Paulson
A: 

protected void Page_Load(object sender, EventArgs e) { string fileType = Request.QueryString["EXT"].ToLower();

        Response.Clear();

        #region Set MIME type
        switch (fileType)
        {
            case "doc":
            case "dot":
                Response.ContentType = "application/msword";
                break;
            case "gif":
            case "tiff":
                Response.ContentType = "image/" + fileType;
                break;
            case "jpg":
                Response.ContentType = "image/jpeg";
                break;
            case "mdb":
                Response.ContentType = "application/x-msaccess";
                break;
            case "mp3":
            case "wav":
                Response.ContentType = "audio/mpeg";
                break;
            case "ppt":
                Response.ContentType = "application/vnd.ms-powerpoint";
                break;
            case "pdf":
            case "rtf":
            case "zip":
                Response.ContentType = "application/" + fileType;
                break;
            case "txt":
                Response.ContentType = "text/plain";
                break;
            case "vsd":
                Response.ContentType = "application/x-visio";
                break;
            case "xls":
                Response.ContentType = "application/vnd.ms-excel";
                break;
            case "xml":
                Response.ContentType = "text/xml";
                break;
            default:
                Response.ContentType = "application/octet-stream";
                break;
        }
        #endregion

        Response.AppendHeader("Content-Disposition", "filename=data." + fileType);

        try
        {
            byte[] response = GetDocument(odUrlQueryString);

Response.BinaryWrite(response); } catch (HttpException ex){ } finally { Response.End(); } }

Sir Psycho
A: 

I have a very good suspicion that its because of the size of the byte array being output. The size is just over 22000 bytes.

Could it be that its too much to send to Response.BinaryWrite() ???

Sir Psycho
No, because then you could never download files over that size. Would it be possible to clean up the answers, potentially move your answers into the original question?
Ray Booysen