我编写了一个 ashx 处理程序,它以安全的方式将文件流式传输到浏览器,我希望授权用户访问这些文件。
问题是当我处理大文件(+40 MB)时,会话消失了+浏览器下载在约 40 MB 后突然中断。
我已将 web.config 配置为在 240 分钟之前不超时。
在本地测试它不会给我同样的问题,在我的共享主机上测试它会。
任何人都可以指出我正确的方向吗?
我尝试了有无 Reponse.Clear()
public void ProcessRequest(HttpContext context)
{
int id;
if (new Core.SecurityManager().CurrentUser != null)
{
try
{
id = Convert.ToInt32(context.Request.QueryString["id"]);
}
catch
{
throw new ApplicationException("id could not be parsed.");
}
string filename = new DocumentFactory().SelectDocumentById(id).Filename;
string filePath = context.Server.MapPath("~/uploads/" + filename);
//context.Response.Clear();
context.Response.AddHeader("content-disposition", "attachment; filename=" + filename);
context.Response.ContentType = "application/octet-stream";
context.Response.WriteFile(filePath);
//context.Response.Flush();
//context.Response.End();
}
else
{
throw new AuthenticationException();
}
}
网络配置:
<sessionState mode="InProc" cookieless="false" timeout="240"></sessionState>
编辑尝试以下,但仍然下载中断:
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
byte[] byteArray = new byte[fs.Length];
using (MemoryStream ms = new MemoryStream(byteArray))
{
long dataLengthToRead = ms.Length;
int blockSize = dataLengthToRead >= 5000 ? 5000 : (int)dataLengthToRead;
byte[] buffer = new byte[dataLengthToRead];
context.Response.Clear();
// Clear the content of the response
context.Response.ClearContent();
context.Response.ClearHeaders();
// Buffer response so that page is sent
// after processing is complete.
context.Response.BufferOutput = true;
// Add the file name and attachment,
// which will force the open/cance/save dialog to show, to the header
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
// bypass the Open/Save/Cancel dialog
//Response.AddHeader("Content-Disposition", "inline; filename=" + doc.FileName);
// Add the file size into the response header
context.Response.AddHeader("Content-Length", fs.Length.ToString());
// Set the ContentType
context.Response.ContentType = "application/octet-stream";
// Write the document into the response
while (dataLengthToRead > 0 && context.Response.IsClientConnected)
{
Int32 lengthRead = ms.Read(buffer, 0, blockSize);
context.Response.OutputStream.Write(buffer, 0, lengthRead);
//Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
context.Response.Flush();
context.Response.Close();
}
// End the response
context.Response.End();
通过添加完整路径通过浏览器直接访问文件时,下载什么都没有问题。