我寫了一個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();
}
}
Web.config文件:
<sessionState mode="InProc" cookieless="false" timeout="240"></sessionState>
編輯嘗試以下,但仍然在下載中斷:
的FileStream FS =新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();
通過添加完整路徑通過瀏覽器直接進入文件時,沒有任何問題,因此不斷下載。
步驟1至3沖洗是一個沒有去因爲我在共享主機上,所以限制IIS設置對我開放。第4步:我需要會話。 – Hifilover
第6步到第9步我試過了,但沒有allas:查看問題更新.. – Hifilover
http://stackoverflow.com/questions/7948316/how-resume-able-file-downlaod-in-asp-net-with- c-sharp-best-way-for-large-fil 訪問此鏈接。這可能會幫助你。 –