2016-09-22 51 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解決了問題。不需要刷新響應。