Solved

Download file with progress bar

Posted on 2008-10-01
4
839 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
  • 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Default parameter problem in C# 3 27
Round a string to two digits 12 25
XML & .net 5 21
Cannot upload files above 1mb IIS7 11 17
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now