Hi,

I'm in the process of writing an aspx page to download a file and I've hit a
serious problem with IsClientConnected. I've appended my stripped down test
page below by way of demonstration.

If I run the code below the browser begins to download the page and if it
completes the file downloads successfully.

If, however, I cancel the download the page continues to run to completion.
It's a shame that HttpResponse.WriteFile doesn't return the number of
characters sent or throw an exception. The end result is that I don't know
if the client received the file or not.

I then tried IsClientConnected (commented out in the code below).
Unfortunately   this blocks indefinately.

Has anyone else experienced this? Does anyone know if this issue relates to
the application configuration settings perhaps?

I'd really appreciate any thoughts or suggestions as this is a showstopper.
The only other thing I can think of is going the IHTTPHandler/ashx route but
I'm not certain that it will get me any further.

Regards,

Martin

--------- download.aspx -----------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Threading;
using System.Diagnostics;

using System.IO;

namespace DownloadSample
{
        /// <summary>
        /// Summary description for Download.
        /// </summary>
        public class Download : System.Web.UI.Page
        {
                // e.g. http://localhost/DownloadSample/Download.aspx

                string strFile = "c:\\bigfile.zip";

                private void Page_Load(object sender, System.EventArgs e)
                {
                        HttpResponse myResponse =
HttpContext.Current.Response;
                        FileInfo myFile = new System.IO.FileInfo(strFile);

                        myResponse.Clear();
                        myResponse.BufferOutput = false;

                        myResponse.AddHeader("Content-Disposition",
"attachment; filename=\"" + "myfile.zip" + "\"");
                        myResponse.AddHeader("Content-Length",
myFile.Length.ToString());
                        myResponse.ContentType = "application/octet-stream";

                        TransmitFile(myResponse, strFile, 0);
                }

                protected long GetTransferRate()
                {
                        return 1024 * 200;
                }

                protected void TransmitFile(HttpResponse response, string
file, long start)
                {
                        FileInfo myFile = new System.IO.FileInfo(file);

                        long size = myFile.Length;

                        long sent = 0;
                        long rate = -1;
                        long todo = -1;

                        int id = AppDomain.GetCurrentThreadId();

                        //want to use response.IsClientConnected but it
blocks
                        try
                        {
                                while ( sent < size /* &&
response.IsClientConnected */ )
                                {
                                        rate = GetTransferRate();
                                        todo = Math.Min(size - sent, rate);

                                        response.WriteFile(file, start +
sent, todo);
                                        response.Flush();

                                        sent += todo;
                                }
                        }
                        catch ( Exception e )
                        {
                                Debug.WriteLine("Exception in
TransmitFile");
                        }
                }

                #region Web Form Designer generated code
                override protected void OnInit(EventArgs e)
                {
                        //
                        // CODEGEN: This call is required by the ASP.NET Web
Form Designer.
                        //
                        InitializeComponent();
                        base.OnInit(e);
                }

                /// <summary>
                /// Required method for Designer support - do not modify
                /// the contents of this method with the code editor.
                /// </summary>
                private void InitializeComponent()
                {
                        this.Error += new System.EventHandler(this.OnError);
                        this.Unload += new
System.EventHandler(this.OnUnload);
                        this.Load += new
System.EventHandler(this.Page_Load);

                }
                #endregion

                private void OnError(object sender, System.EventArgs e)
                {
                        Debug.WriteLine("OnError fired");
                }

                private void OnUnload(object sender, System.EventArgs e)
                {
                        Debug.WriteLine("OnUnload fired");
                }
        }
}
-----------------------------------

You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to