Jawad Khan

Jawad's Lodge - The willingness to torture yourself before others is what makes a developer truly a unique breed.
posts - 45, comments - 149, trackbacks - 155

My Links

News

Archives

Post Categories

Image Galleries

How to make sure that the file is downloaded completely by the Users of Web Application ...

While developing a Website you might want to limit the users to download the file only once or would like to hide the link if they successfully downloaded a particular file. In traditional way of downloading the file its hard to know on the server side that file is downloaded successfully by the client. Solution is to stream the file in small chunk and make sure that is connected before streaming the next chunk. If you sucessfully transmitted the whiole file then this would mean that client has successfully downloaded the file.

Here is the Code ....

 public class DownloadFile : System.Web.UI.Page
 {
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   System.IO.Stream iStream = null;

   // Buffer to read 10K bytes in chunk:
   byte[] buffer = new Byte[10000];

   // Length of the file:
   int length;

   // Total bytes to read:
   long dataToRead;

   // Identify the file to download including its path.
   string filepath  = Request.MapPath("TSDCJan21.sdo");

   // Identify the file name.
   string  filename  = System.IO.Path.GetFileName(filepath);

   try
   {
    // Open the file.
    iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
     System.IO.FileAccess.Read,System.IO.FileShare.Read);


    // Total bytes to read:
    dataToRead = iStream.Length;

    Response.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

    // Read the bytes.
    while (dataToRead > 0)
    {
     // Verify that the client is connected.
     if (Response.IsClientConnected)
     {
      // Read the data in buffer.
      length = iStream.Read(buffer, 0, 10000);

      // Write the data to the current output stream.
      Response.OutputStream.Write(buffer, 0, length);

      // Flush the data to the HTML output.
      Response.Flush();

      buffer= new Byte[10000];
      dataToRead = dataToRead - length;
     }
     else
     {
      //prevent infinite loop if user disconnects
      dataToRead = -1;
     }
    }
   }
   catch (Exception ex)
   {
    // Trap the error, if any.
    Response.Write("Error : " + ex.Message);
   }
   finally
   {
    if (iStream != null)
    {
     //Close the file.
     iStream.Close();
    }
   }

  }

 }

Print | posted on Tuesday, November 01, 2005 12:00 PM | Filed Under [ ASP.NET ]

Feedback

Gravatar

# re: How to make sure that the file is downloaded completely by the Users of Web Application ...

Although it's technically doable I thinkg you should never do it. There are few things more frustrating than loosing a file and not being able to get it once more.
7/11/2007 3:23 AM | Michal Talaga
Gravatar

# re: How to make sure that the file is downloaded completely by the Users of Web Application ...

Hi Michal,
I am not sure what you are referring too when you say "loosing a file". If you are downloading a file the chances are you don't have that file and on the server side streaming an existing file will not harm that file in any way. Bottom line ine either case there is no loss of existing file(s).
7/11/2007 11:19 AM | Jawad Khan
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: