本文介绍了使用ASP.NET MVC下载后如何删除文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要在下载后立即删除文件,该怎么办?我尝试子类化FilePathResult
并覆盖
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 I delete file after
HttpResponseBase.TransmitFile
被调用,但这会挂起应用程序.
is called, but this hangs the application.
用户下载后可以安全删除文件吗?
Can I safely delete a file after user downloads it?
推荐答案
您可以使用OnActionExecuted方法为操作创建自定义操作过滤器,该方法将在操作完成后删除文件,例如
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
}
}
然后您的动作
[DeleteFileAttribute]
public FileContentResult GetFile(int id)
{
...
}
这篇关于使用ASP.NET MVC下载后如何删除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!