.net - 在 MVC asp.net 中的客户端 PC 上完成下载后,如何删除文件

标签 .net asp.net-mvc asp.net-mvc-3 download

我已经使用 getCSV 函数在客户端返回 csv 文件,但我想在客户端下载完成后删除一个文件

我的功能

Public Function GetCSVFile(ByVal listSeperator As String) As CSVResult
    Dim result As Boolean
    Dim Service As New Service()
    Dim types As IList(Of ProductType) = Service.GetAllWithAllLocalizedNames(New ListOptions(Name", OrderByDirection.Ascending, 1, -1))
    Dim path As String, colname As String
    colname = Settings.GetLanguage()
    path = Common.GetLocalizeDataPath()
    Dim seprator As String = Common.GetListSepratorForCSV(listSeperator)
    Dim cs As New CSVGenerator(path, colname, seprator)
    result = cs.CreateCSVFile(types)
    If result Then
        Return New CSVResult With {.FileName = "Sample.csv", .Path = "~/Mydir/Sample.csv"}
    Else
        Return New CSVResult With {.FileName = "Error.csv", .Path = "~/Mydir/Error.csv"}
    End If
End Function

最佳答案

仅当您刷新响应时才对我有用。属性过滤器应该是这样的

    public class DeleteTempFileResultFilter : ActionFilterAttribute
    {
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            string fileName = ((FilePathResult)filterContext.Result).FileName;

            filterContext.HttpContext.Response.Flush();
            filterContext.HttpContext.Response.End();

            System.IO.File.Delete(fileName);
        }
    }

关于.net - 在 MVC asp.net 中的客户端 PC 上完成下载后,如何删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5537838/

相关文章:

.net - 如何在vb.net中对结构数组的相似元素求和?

c# - 无限期地在任务上调用 .ContinueWith 是模拟队列和循环线程的有效方法吗?

c# - MVC 局部 View

c# - 使用包含逗号的数据写入 MVC 中的 csv

asp.net-mvc-3 - 在 MVC3 中,如何通过 ActionLink 将多个值传递给查询字符串参数?

.net - LINQ 表达式 TreeMap 灵完整吗?

c# - .NET 的免费 UML 绘图库

c# - 无法在 Asp.NET MVC 中命中 Api 路由

c# - Razor 的文本显示太长

asp.net-mvc - 在局部 View 中渲染 umbraco 场