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 7595285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:38:08+00:00 2026-05-30T21:38:08+00:00

Hi my download function. protected void downloadFunction(string fileName) { string filePath = @"D:\SoftwareFiles\"; LogMessageToFile("Download

  • 0

Hi my download function.

protected void downloadFunction(string fileName)
{
    string filePath = @"D:\SoftwareFiles\";
    LogMessageToFile("Download started " + filePath + fileName);
    byte[] array = File.ReadAllBytes(filePath + fileName);
        

    Response.Clear();
    Response.ContentType = "application/x-newton-compatible-pkg";
    Response.AppendHeader("Content-Disposition", 
                          "attachment;filename=" + fileName);

    Response.BinaryWrite(array);
    Response.End();
}

When handling filesize of 20, 200mb no problem.

When handling 1gb file, an exception is thrown:

Overflow or underflow in the arithmetic operation.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArithmeticException: Overflow or underflow in the arithmetic operation.

What to do?

  • 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-30T21:38:09+00:00Added an answer on May 30, 2026 at 9:38 pm

    My guess is that you’re running out of memory in the byte[] array.

    You can try breaking the file down and reading it in chunks.

    I found a code example from a Google search to get you started:

    C# file downloader AKA Response.BinaryWrite

    using System;
    using System.IO;
    using System.Web;
    
    public class Download
    {
        public static void SmallFile(string filename, string filepath, string contentType)
        {
            try
            {
                FileStream MyFileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
                long FileSize;
                FileSize = MyFileStream.Length;
                byte[] Buffer = new byte[(int)FileSize];
                MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length);
                MyFileStream.Close();
                HttpContext.Current.Response.ContentType = contentType;
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
                HttpContext.Current.Response.BinaryWrite(Buffer);             
            }
            catch
            {
                HttpContext.Current.Response.ContentType = "text/html";
                HttpContext.Current.Response.Write("Downloading Error!");
            }
            HttpContext.Current.Response.End();
        }
    
        public static void LargeFile(string filename, string filepath, string contentType)
        {
            Stream iStream = null;
            // Buffer to read 10K bytes in chunk
            //byte[] buffer = new Byte[10000];
            // 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);
                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();             
            }         
        }
    
        public static void ResumableFile(string filename, string fullpath, string contentType)
        {
            try
            {
                FileStream myFile = new FileStream(fullpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader br = new BinaryReader(myFile);
                try
                {
                    HttpContext.Current.Response.AddHeader("Accept-Ranges", "bytes");
                    HttpContext.Current.Response.Buffer = false;
                    long fileLength = myFile.Length;
                    long startBytes = 0;
    
                    //int pack = 10240; //10K bytes
                    int pack = 1048576; //1024K bytes
    
                    if (HttpContext.Current.Request.Headers["Range"] != null)
                    {
                        HttpContext.Current.Response.StatusCode = 206;
                        string[] range = HttpContext.Current.Request.Headers["Range"].Split(new char[] { '=', '-' });
                        startBytes = Convert.ToInt64(range[1]);
                    }
                    HttpContext.Current.Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
                    if (startBytes != 0)
                    {
                        HttpContext.Current.Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
                    }
                    HttpContext.Current.Response.AddHeader("Connection", "Keep-Alive");
                    HttpContext.Current.Response.ContentType = contentType;
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
    
                    br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
                    int maxCount = (int)Math.Floor((double)((fileLength - startBytes) / pack)) + 1;
    
                    for (int i = 0; i < maxCount; i++)
                    {
                        if (HttpContext.Current.Response.IsClientConnected)
                        {
                            HttpContext.Current.Response.BinaryWrite(br.ReadBytes(pack));
                        }
                        else
                        {
                            i = maxCount;
                        }
                    }
                }
                catch
                {
                    HttpContext.Current.Response.ContentType = "text/html";
                    HttpContext.Current.Response.Write("Error : file not found");
                }
                finally
                {
                    br.Close();
                    myFile.Close();
                }
            }
            catch
            {
                HttpContext.Current.Response.ContentType = "text/html";
                HttpContext.Current.Response.Write("Error : file not found");
            }
        }     
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

protected void downloadFunction(string filename) { string filepath = @D:\XtraFiles\ + filename; string contentType =
I've a function to download a file from a remote URL (using Java). Now
I need a simple proxy PHP function/script that can download a file from a
When implementing download function it work but during file saving to sdcard i get
Warning: include(html\download.html) [function.include]: failed to open stream: No such file or directory in /home/jamia/public_html/download.php
I'm looking to add a Download this File function below every video on one
I'm trying to force a download of a protected zip file (I don't want
I created a download function in php in which the file contains special characters
i download a kml file : <?xml version=1.0 encoding=UTF-8?> <kml xmlns=http://www.opengis.net/kml/2.2> <Document> <Style id=transGreenPoly>
The following code executes a function that retrieves a file via ftp and then

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.