C# 下载功能因文件大小过大而失败
嗨,我的下载功能C# 下载功能因文件大小过大而失败,c#,asp.net,.net,C#,Asp.net,.net,嗨,我的下载功能 protected void downloadFunction(string fileName) { string filePath = @"D:\SoftwareFiles\"; LogMessageToFile("Download started " + filePath + fileName); byte[] array = File.ReadAllBytes(filePath + fileName);
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();
}
处理文件大小为20,200mb时没有问题
处理1gb文件时,会引发异常:
算术运算中的溢出或下溢
描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源
异常详细信息:System.ArrithmeticException:算术运算中的溢出或下溢
怎么办?我猜
字节[]数组中的内存不足
您可以尝试将文件分解并分块读取
我在谷歌搜索中找到了一个代码示例,让您开始:
使用系统;
使用System.IO;
使用System.Web;
公共类下载
{
公共静态void SmallFile(字符串文件名、字符串文件路径、字符串内容类型)
{
尝试
{
FileStream MyFileStream=newfilestream(filepath,FileMode.Open,FileAccess.Read,FileShare.Read);
长文件大小;
FileSize=MyFileStream.Length;
字节[]缓冲区=新字节[(int)文件大小];
读取(缓冲区,0,(int)MyFileStream.Length);
MyFileStream.Close();
HttpContext.Current.Response.ContentType=ContentType;
HttpContext.Current.Response.AddHeader(“内容处置”、“附件;文件名=“+HttpUtility.UrlEncode(文件名,System.Text.Encoding.UTF8));
HttpContext.Current.Response.BinaryWrite(缓冲区);
}
抓住
{
HttpContext.Current.Response.ContentType=“text/html”;
HttpContext.Current.Response.Write(“下载错误!”);
}
HttpContext.Current.Response.End();
}
公共静态void LargeFile(字符串文件名、字符串文件路径、字符串内容类型)
{
流iStream=null;
//块中读取10K字节的缓冲区
//字节[]缓冲区=新字节[10000];
//用于读取区块中1024K字节的缓冲区
字节[]缓冲区=新字节[1048576];
//文件的长度:
整数长度;
//要读取的总字节数:
长数据存储;
尝试
{
//打开文件。
iStream=新文件流(filepath,FileMode.Open,FileAccess.Read,FileShare.Read);
//要读取的总字节数:
dataToRead=iStream.Length;
HttpContext.Current.Response.ContentType=ContentType;
HttpContext.Current.Response.AddHeader(“内容处置”、“附件;文件名=“+HttpUtility.UrlEncode(文件名,System.Text.Encoding.UTF8));
//读取字节。
而(数据读取>0)
{
//验证客户端是否已连接。
if(HttpContext.Current.Response.IsClientConnected)
{
//读取缓冲区中的数据。
长度=iStream.Read(缓冲区,0,10000);
//将数据写入当前输出流。
HttpContext.Current.Response.OutputStream.Write(缓冲区,0,长度);
//将数据刷新到HTML输出。
HttpContext.Current.Response.Flush();
缓冲区=新字节[10000];
dataToRead=dataToRead-长度;
}
其他的
{
//如果用户断开连接,防止无限循环
dataToRead=-1;
}
}
}
捕获(例外情况除外)
{
//捕获错误(如果有)。
//HttpContext.Current.Response.Write(“错误:+ex.Message”);
HttpContext.Current.Response.ContentType=“text/html”;
HttpContext.Current.Response.Write(“错误:找不到文件”);
}
最后
{
如果(iStream!=null)
{
//关闭文件。
iStream.Close();
}
HttpContext.Current.Response.End();
HttpContext.Current.Response.Close();
}
}
公共静态无效可恢复文件(字符串文件名、字符串完整路径、字符串内容类型)
{
尝试
{
FileStream myFile=newfilestream(完整路径,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
BinaryReader br=新的BinaryReader(myFile);
尝试
{
HttpContext.Current.Response.AddHeader(“接受范围”、“字节”);
HttpContext.Current.Response.Buffer=false;
long fileLength=myFile.Length;
长起始字节=0;
//int pack=10240;//10K字节
int pack=1048576;//1024K字节
if(HttpContext.Current.Request.Headers[“Range”]!=null)
{
HttpContext.Current.Response.StatusCode=206;
string[]range=HttpContext.Current.Request.Headers[“range”].Split(新字符[]{'=','-});
startBytes=Convert.ToInt64(范围[1]);
}
HttpContext.Current.Response.AddHeader(“内容长度”(fileLength-startBytes.ToString());
如果(起始字节!=0)
{
HttpContext.Current.Response.AddHeader(“内容范围”,string.Format(“字节{0}-{1}/{2}”,起始字节,文件长度-1,文件长度));
}
HttpContext.Current.Response.AddHeader(“连接”,“保持活动”);
HttpContext.Current.Response.ContentType=ContentType;
HttpContext.Current.Response.AddHeader(“内容处置”、“附件;文件名=“+HttpUtility.UrlEncode(文件名,System.Text.Encoding.UTF8));
br.BaseStream.Seek(startBytes,SeekOrigin.Begin);
int maxCo
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");
}
}
}