Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7598395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:23:49+00:00 2026-05-30T22:23:49+00:00

protected void downloadFunction(string filename) { string filepath = @D:\XtraFiles\ + filename; string contentType =

  • 0
protected void downloadFunction(string filename)
{
    string filepath = @"D:\XtraFiles\" + filename;
    string contentType = "application/x-newton-compatible-pkg";

    Stream iStream = null;
    // Buffer to read 1024K bytes in chunk
    byte[] buffer = new Byte[1048576];

    // Length of the file:
    int length;
    // Total bytes to read:
    long dataToRead;

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

        // Total bytes to read:
        dataToRead = iStream.Length;
        HttpContext.Current.Response.ContentType = contentType;
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));

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

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

                // Flush the data to the HTML output.
                HttpContext.Current.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.
        HttpContext.Current.Response.Write("Error : " + ex.Message + "<br />");
        HttpContext.Current.Response.ContentType = "text/html";
        HttpContext.Current.Response.Write("Error : file not found");
    }
    finally
    {
        if (iStream != null)
        {
            //Close the file.
            iStream.Close();
        }
        HttpContext.Current.Response.End();
        HttpContext.Current.Response.Close();
    }
}

My donwload function is working perfect, but when users are downloading the browser cant see the total file size of the download.

So now the browser says eq. Downloading 8mb of ?, insted of Downloading 8mb of 142mb.

What have i missed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:23:50+00:00Added an answer on May 30, 2026 at 10:23 pm

    The Content-Length header seems to be what you are missing.

    If you set this the browser will then know how much to expect. Otherwise it will just keep going til you stop sending data and it won’t know how long it is until the end.

    Response.AddHeader("Content-Length", iStream.Length);
    

    You may also be interested in Response.WriteFile whcih can provide an easier way to send a file to a client without having to worry about streams yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi my download function. protected void downloadFunction(string fileName) { string filePath = @"D:\SoftwareFiles\"; LogMessageToFile("Download
Page Load: protected void Page_Load(object sender, EventArgs e) { tb.Columns.Add(Id, typeof(int)); tb.Columns.Add(FileName, typeof(string)); tb.Columns.Add(FilePath,
protected void imgbtn_BBDownload_Click(object sender, EventArgs e) { Response.ContentType = application/exe; Response.AppendHeader(Content-Disposition,attachment; filename=bb.exe); Response.TransmitFile( Server.MapPath(~/Resources/bb.exe)
protected void Button1_Click(object sender, EventArgs e) { Response.ContentType = text/txt; Response.AppendHeader(Content-Disposition, attachment; filename= +
protected void LoginCheck() { if (Session["Login_Status"] == null || Session["Login_Status"] == "false") { MessageBoxShow("Please
protected void Button2_Click(object sender, EventArgs e) { string ad = TextBox1.Text; string firma =
protected void displayNotification(String response) { Intent intent = new Intent(context, testActivity.class); PendingIntent pendingIntent =
protected void Page_Load(object sender, EventArgs e) { string menuIDdata = Page.Request.QueryString[mid]; menuID = 0;
Global.asax Code protected void Application_BeginRequest(object sender, EventArgs e) { string fullOrigionalpath = Request.Url.ToString(); if
The following function works perfectly: protected void SendWebMailMessage(string DisplayName, string From, string ReplyTo, string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.