【发布时间】:2016-09-22 08:45:52
【问题描述】:
我正在尝试删除在我的控制器上使用 ActionFilter 动态生成的文件。
public class DeleteFileAttribute : ActionFilterAttribute {
public override void OnActionExecuted(ActionExecutedContext context) {
/* MVC 4 code which worked just fine
context.HttpContext.Response.Flush();
var filePathResult = context.Result as FilePathResult;
if(filePathResult!=null){
File.Delete(filePathResult.FileName);
}
End of MVC 4 code */
// I am not able to flush response as context.HttpContext.Response does not have a Flush method
var vb = (context.Controller as Controller).ViewBag;
string fileToDelete = vb.FileToDelete;
if(File.Exists(fileToDelete)) {
// ERROR: Process cannot access the file ... because it is being used by another process
File.Delete(fileToDelete);
}
}
}
如代码注释中所述,由于我无法刷新响应然后删除文件,因此我得到了IOException: The process cannot access the file ... because it is being used by another process 异常。
在用户完成下载后,在操作过滤器中删除文件的可靠方法是什么?
【问题讨论】:
标签: c# asp.net .net asp.net-core .net-core