Valentin Valentin - 1 year ago 340
C# Question

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

I want to delete a file immediately after download, how do I do it? I've tried to subclass

and override the
method where I delete file after


is called, but this hangs the application.

Can I safely delete a file after user downloads it?

Answer Source

You could create a custom actionfilter for the action with an OnActionExecuted Method that would then remove the file after the action was completed, something like

public class DeleteFileAttribute : ActionFilterAttribute 
    public override void OnActionExecuted(ActionExecutedContext filterContext) 
        // Delete file 

then your action has

public FileContentResult GetFile(int id)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download