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
Read in the bytes of the file, delete it, call the base controller's File action.
public class MyBaseController : Controller
{
protected FileContentResult TemporaryFile(string fileName, string contentType, string fileDownloadName)
{
var bytes = System.IO.File.ReadAllBytes(fileName);
System.IO.File.Delete(fileName);
return File(bytes, contentType, fileDownloadName);
}
}
BTW, you may refrain from this method if you're dealing with very large files, and you're concerned about the memory consumption.