2016-09-22 86 views
3

我正试图删除一个文件,该文件是使用ActionFilter在我的控制器中动态生成的。如何刷新asp.net核心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例外。

用户完成下载后,在操作筛选器中删除文件的方法是什么?

回答

0

在动作过滤器中使用OnResultExecuted事件而不是OnActionExecuted解决了问题。不需要刷新响应。

相关问题