2016-09-22 2 views
3

ActionFilter를 사용하여 컨트롤러에서 동적으로 생성 된 파일을 삭제하려고합니다. 나는 응답을 세척 한 후 내가 IOException: The process cannot access the file ... because it is being used by another process 예외가 파일을 삭제할 수 없습니다입니다 때문에, 코드 주석에서 언급 한 바와 같이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); 
     } 
    } 
} 

.

사용자가 다운로드를 완료 한 후 작업 필터에서 파일을 삭제하는 확실한 방법은 무엇입니까?

답변

0

동작 필터에서 OnActionExecuted 대신 OnResultExecuted 이벤트를 사용하면이 문제가 해결되었습니다. 응답을 내 보내지 않아도됩니다.

관련 문제