gpt4 book ai didi

c# - 下载功能失败,文件大小为 1.35gb

转载 作者:行者123 更新时间:2023-11-30 18:35:08 27 4
gpt4 key购买 nike

我有这个下载功能,而且效果很好。但是对于文件大小为 1.35gb 的文件,下载会停止在 300 Mb、382、400mb 或 1.27 Gb。我究竟做错了什么? (下载功能做成这样,因为文件需要隐藏,不能在网站上公开。)

public static void downloadFunction(string filename)
{
string filepath = @"D:\texts\New folder\DLfolder\" + filename;
string contentType = "application/x-newton-compatible-pkg";

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));
HttpContext.Current.Response.AddHeader("Content-Length", iStream.Length.ToString());

// 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 + "<br />");

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

最佳答案

您可能已达到请求超时。请注意,如果文件在预定时间后停止下载,例如 60 或 300 秒。如果是这种情况,您可以在应用程序的 web.config 中配置超时。

关于c# - 下载功能失败,文件大小为 1.35gb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15384407/

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