gpt4 book ai didi

c# - 算术运算导致溢出错误 - 对于 c# 中的 byte[] 数组

转载 作者:太空宇宙 更新时间:2023-11-03 18:41:45 37 4
gpt4 key购买 nike

我在 C# 中有这一行:

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

该长度返回大于 4 GB 的文件大小。

在那一行我有以下错误:

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);

我该如何解决这个错误?

编辑
我正在使用 .net 4
该代码是下载文件处理程序的一部分,如下所示:

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;
}
}
}
}

提前致谢

最佳答案

.NET 中的数组不能容纳超过 2^31 个元素(System.Int32.MaxValue)或 a max size of 2 GB这大致构成了一个 2 GB 的字节数组。

有关解决方法,请参阅 http://blogs.msdn.com/b/joshwil/archive/2005/08/10/450202.aspx

另一种选择是使用 MemoryMappedFileStreams在那些 - 这将允许访问任何大小的文件...

要使下载代码正常工作,您可以读取一个 block 、发送它、读取下一个 block 等。或者使用Stream 读取和写入OutputStream。没有任何中间缓冲...

另一种选择是使用 TransmitFile可以处理大于 4 GB 的文件。

编辑 - 根据评论:

你可以在

之后替换代码
if (filePath == null)
{
return;
}

response.Clear();

response.ContentType = GetMimeType (System.IO.Path.GetFileName(filePath));

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

response.TransmitFile (filePath);

或与

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();
}

关于c# - 算术运算导致溢出错误 - 对于 c# 中的 byte[] 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8070949/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com