How to delete file after download with ASP.NET MVC?

后端 未结 13 1016
执念已碎
执念已碎 2020-12-07 20:51

I want to delete a file immediately after download, how do I do it? I\'ve tried to subclass FilePathResult and override the WriteFile method where

13条回答
  •  [愿得一人]
    2020-12-07 21:45

    overriding the OnResultExecuted method is probably the correct solution.. This method runs after the response is written.

    public class DeleteFileAttribute : ActionFilterAttribute 
    { 
        public override void OnResultExecuted(ResultExecutedContext filterContext) 
        { 
            filterContext.HttpContext.Response.Flush();
            // Delete file 
        } 
    } 
    

    Action code:

    [DeleteFileAttribute]
    public FileContentResult GetFile(int id)
    {
       //your action code
    }
    

提交回复
热议问题