У меня есть эта строка в c #:
byte[] bytes = new byte[streamReader.BaseStream.Length];
Эта длина возвращает размер файла больше 4 ГБ.
в этой строке у меня ошибка ниже:
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);
как я могу исправить эту ошибку?
edit
я использую .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;
}
}
}
}
заранее спасибо