我有以下代码:
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共享主机)?谢谢。
答案 0 :(得分:3)
1)我认为您应该在TransmitFile()之后放置context.Response.Flush(),以确保在继续删除文件之前将文件流式传输到客户端。
2)TransferFile()也流式传输文件而不在内存中缓冲它。这将在文件正在使用时锁定该文件。您可能希望将其加载到内存并使用Response.OutputStream。
答案 1 :(得分:0)
我已经从GoDaddy转移到Arvixe(原因:发送电子邮件时遇到很大问题),更改了代码,直到现在一切正常(成功和中断下载):
try
{
context.Response.TransmitFile(savedFileName);
}
catch (Exception ex)
{
ExceptionUtility.LogException(new Exception("Exception while transmit zip [AttachmentsSecurityHandler.cs]: " + ex.Message), false);
}
finally
{
byte attemptsCounter = 0;
Thread deletingThread = new Thread(delegate()
{
while (attemptsCounter < 5)
{
Thread.Sleep(5000);
try
{
File.Delete(savedFileName);
break;
}
catch (Exception ex)
{
attemptsCounter++;
ExceptionUtility.LogException(new Exception(string.Format("Unable to delete temp zip file [AttachmentsSecurityHandler.cs] (attempt {0}): {1}", attemptsCounter, ex.Message)), false);
}
}
});
deletingThread.Start();
}