?
Solved

Download file with progress bar

Posted on 2008-10-01
4
Medium Priority
?
852 Views
Last Modified: 2013-12-17
Hi

I used the following code to let user download a file. It does work. However the download dialog box does not show its download progress in a bar.

I use C# and ASP.NET to build it and I download it from IE6.

any help?
System.IO.FileInfo file = new FileInfo(filepath);
                if (file.Exists)
                {
                    System.IO.Stream iStream = null;
                    byte[] buffer = new Byte[10000];
                    int length;
                    long dataToRead;
                    string filename = System.IO.Path.GetFileName(filepath);
                    try
                    {
                        iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
                                    System.IO.FileAccess.Read, System.IO.FileShare.Read);
                        dataToRead = iStream.Length;
                        Response.ContentType = "application/octet-stream";
                        Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
                        while (dataToRead > 0)
                        {
                            if (Response.IsClientConnected)
                            {
                                length = iStream.Read(buffer, 0, 1000000);
                                Response.OutputStream.Write(buffer, 0, length);
                                Response.Flush();
                                buffer = new Byte[10000];
                                dataToRead = dataToRead - length;                                
                            }
                            else
                            {
                                dataToRead = -1;
                            }
                        }
                    }

Open in new window

0
Comment
Question by:techques
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Author Comment

by:techques
ID: 22626497
How to fix it?
0
 
LVL 3

Expert Comment

by:tasky
ID: 22626677
You can try changing the content type to

application/force-download

Let me know if that has any effect.
0
 

Accepted Solution

by:
techques earned 0 total points
ID: 22631091
No, it should be

Response.AddHeader("Content-Length", data.ToString());
0
 
LVL 3

Expert Comment

by:tasky
ID: 22662167
Actually, it should be the length of the stream, not the value of the data...
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Change in PHP Behavior with Session Write Short Circuit (http://php.net/manual/en/book.session.php#116217) (Winter 2014)** With the release of PHP 5.6 the session handler changed in a way that many think should be considered a bug.  See the note …
New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question