我有以下代碼:ASP.NET刪除文件
try
{
context.Response.TransmitFile(savedFileName);
}
catch (Exception ex)
{
ExceptionUtility.LogException(new Exception("Exception while transmit zip [AttachmentsSecurityHandler.cs]: " + ex.Message), false);
}
finally
{
try
{
Thread.Sleep(500);
File.Delete(savedFileName);
}
catch (Exception ex)
{
ExceptionUtility.LogException(new Exception("Unable to delete temp zip file [AttachmentsSecurityHandler.cs]: " + ex.Message), false);
}
}
一切正常,只有當用戶取消下載我得到:
Exception while transmit zip [AttachmentsSecurityHandler.cs]: The remote host closed the connection. The error code is 0x800703E3.
Unable to delete temp zip file [AttachmentsSecurityHandler.cs]: The process cannot access the file 'D:\Hosting\***\html\attachments\tempCompressed\b9b5c47e-86f9-4610-9293-3b92dbaee222' because it is being used by another process.
只刪除孤立的文件方式是試圖刪除舊的(例如一個小時)文件?系統將鎖定多長時間(GoDaddy共享主機)?謝謝。
感謝您的回答。我試過了,沒有區別。我使用context.Response.Buffer = false;和context.Response.BufferOutput = false;文件可能很大,所以我不想將文件內容放入內存中。正如我之前所說 - 代碼很好,當用戶不取消下載。 – Mateusz