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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:46:17+00:00 2026-05-26T16:46:17+00:00

i have this line in c# : byte[] bytes = new byte[streamReader.BaseStream.Length]; That Length

  • 0

i have this line in c# :

    byte[] bytes = new byte[streamReader.BaseStream.Length];

That Length returns a file size bigger than 4 GB.

at that line i have the Error below :

Arithmetic operation resulted in an overflow. 
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.OverflowException: Arithmetic operation resulted in an overflow.

Source Error: 


Line 41:             System.IO.BinaryReader br = new System.IO.BinaryReader(streamReader.BaseStream);
Line 42: 
Line 43:             byte[] bytes = new byte[streamReader.BaseStream.Length];
Line 44: 
Line 45:             br.Read(bytes, 0, (int)streamReader.BaseStream.Length);

how can i fix this error ?

edit
i am using .net 4
that code was part of a handler for download files like below :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using WindowsServer.Classes;

namespace WindowsServer
{
    /// <summary>
    /// Summary description for HandlerForMyFE
    /// </summary>
    public class Handler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        private HttpContext _context;
        private HttpContext Context
        {
            get
            {
                return _context;
            }
            set
            {
                _context = value;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            Context = context;
            string filePath = context.Request.QueryString["Downloadpath"];
            filePath = context.Server.MapPath(filePath);

            if (filePath == null)
            {
                return;
            }

            System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath);
            System.IO.BinaryReader br = new System.IO.BinaryReader(streamReader.BaseStream);

            byte[] bytes = new byte[streamReader.BaseStream.Length];

            br.Read(bytes, 0, (int)streamReader.BaseStream.Length);

            if (bytes == null)
            {
                return;
            }

            streamReader.Close();
            br.Close();
            string fileName = System.IO.Path.GetFileName(filePath);
            string MimeType = GetMimeType(fileName);
            string extension = System.IO.Path.GetExtension(filePath);
            char[] extension_ar = extension.ToCharArray();
            string extension_Without_dot = string.Empty;
            for (int i = 1; i < extension_ar.Length; i++)
            {
                extension_Without_dot += extension_ar[i];
            }

            string filesize = string.Empty;
            FileInfo f = new FileInfo(filePath);
            filesize = f.Length.ToString();

            //DownloadFile.DownloadFileMethod_2(Context, filePath, 5242880);
              WriteFile(bytes, fileName, filesize, MimeType + " " + extension_Without_dot, context.Response);
        }

       private void WriteFile(byte[] content, string fileName, string filesize, string contentType, HttpResponse response)
    {
        response.Buffer = true;
        response.Clear();

        response.ContentType = contentType;

        response.AddHeader("content-disposition", "attachment; filename=" + fileName);

        response.AddHeader("Content-Length", filesize);

        response.BinaryWrite(content);
        response.Flush();
        response.End();
    }

        private string GetMimeType(string fileName)
        {
            string mimeType = "application/unknown";
            string ext = System.IO.Path.GetExtension(fileName).ToLower();
            Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
            if (regKey != null && regKey.GetValue("Content Type") != null)
                mimeType = regKey.GetValue("Content Type").ToString();
            return mimeType;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

thanks in advance

  • 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-26T16:46:18+00:00Added an answer on May 26, 2026 at 4:46 pm

    No array in .NET can hold more than 2^31 element (System.Int32.MaxValue) or a max size of 2 GB which roughly would make for a 2 GB byte array.

    For a workaround see http://blogs.msdn.com/b/joshwil/archive/2005/08/10/450202.aspx

    Another option is to use MemoryMappedFile and Streams on those – this will alow to access a file of any size…

    To make that download code work you could either read a chunk, send it, read the next chunk etc. OR use a Stream for reading and write to the OutputStream without any intermediate buffer…

    Another option is to use TransmitFile which can handle files > 4 GB.

    EDIT – as per comment:

    You could just replace the code after

    if (filePath == null)
    {
    return;
    }
    

    with

    response.Clear();
    
    response.ContentType = GetMimeType (System.IO.Path.GetFileName(filePath));
    
    response.AddHeader("content-disposition", "attachment; filename=" + System.IO.Path.GetFileName(filePath));
    
    response.TransmitFile (filePath);    
    

    OR with

    long FileL = (new FileInfo(filePath)).Length;
    byte[] bytes = new byte[1024*1024];
    
    response.Clear();
    
    response.ContentType = GetMimeType (System.IO.Path.GetFileName(filePath));
    
    response.AddHeader("content-disposition", "attachment; filename=" + System.IO.Path.GetFileName(filePath));
    
    response.AddHeader("Content-Length", FileL.ToString());
    
    using (FileStream FS = File.OpenRead(filePath))
    {
    int bytesRead = 0;
    while ((bytesRead = FS.Read (bytes, 0, bytes.Length)) > 0 )
    {
    response.OutputStream.Write(bytes, 0, bytesRead);
    response.Flush();
    };
    
    response.Close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file bigger than 10G. To read this file line by line,
I have this line in a useful Bash script that I haven't managed to
I have this line of JavaScript and the behavior I am seeing is that
I have this line below that shows a link to go the next page
I have some Perl code that translates new-lines and line-feeds to a normalized form.
I have this line in a javascript block in a page: res = foo('<%=
I have this line of code for page load: if ($(input).is(':checked')) { and it
I have this line in the declarations section: Private filePath As String And something
I have this line in my program : InputStream Resource_InputStream=this.getClass().getClassLoader().getResourceAsStream(Resource_Name); But how can I
Say I have this line of code in the view. <?php echo CHtml::activeTextField($model,'start_time'); ?>

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.