如何在 ASP.NET Core 中流式传输文件后删除文件

How to delete a file after it was streamed in ASP.NET Core(如何在 ASP.NET Core 中流式传输文件后删除文件)
本文介绍了如何在 ASP.NET Core 中流式传输文件后删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在返回表单操作后删除一个临时文件.我如何使用 ASP.NET Core 实现这一点:

I would like to delete a temporary file after returning it form action. How can i achieve that with ASP.NET Core:

public IActionResult Download(long id)
{
    var file = "C:/temp/tempfile.zip";
    var fileName = "file.zip;
    return this.PhysicalFile(file, "application/zip", fileName);
    // I Would like to have File.Delete(file) here !!
}

文件太大,无法使用内存流返回.

The file is too big for returning using memory stream.

推荐答案

File() 或 PhysicalFile() 返回一个 FileResult 派生类,它只是 委托处理给一个执行服务.PhysicalFileResult 的 ExecuteResultAsync 方法调用:

File() or PhysicalFile() return a FileResult-derived class that just delegates processing to an executor service. PhysicalFileResult's ExecuteResultAsync method calls :

var executor = context.HttpContext.RequestServices
                 .GetRequiredService<IActionResultExecutor<PhysicalFileResult>>();
return executor.ExecuteAsync(context, this);

所有其他基于 FileResult 的类都以类似的方式工作.

All other FileResult-based classes work in a similar way.

PhysicalFileResultExecutor 类本质上是将文件的内容写入响应流.

The PhysicalFileResultExecutor class essentially writes the file's contents to the Response stream.

一个快速而肮脏的解决方案是创建您自己的基于 PhysicalFileResult 的类,该类委托给 PhysicalFileResultExecutor,但在执行程序完成后删除文件:

A quick and dirty solution would be to create your own PhysicalFileResult-based class that delegates to PhysicalFileResultExecutor but deletes the file once the executor finishes :

public class TempPhysicalFileResult : PhysicalFileResult
{
    public TempPhysicalFileResult(string fileName, string contentType)
                 : base(fileName, contentType) { }
    public TempPhysicalFileResult(string fileName, MediaTypeHeaderValue contentType)
                 : base(fileName, contentType) { }

    public override async  Task ExecuteResultAsync(ActionContext context)
    {
        try {
            await base.ExecuteResultAsync(context);
        }
        finally {
            File.Delete(FileName);
        }
    }
}

您可以创建并返回 TempPhysicalFileResult,而不是调用 PhysicalFile() 来创建 PhysicalFileResult,例如:

Instead of calling PhysicalFile() to create the PhysicalFileResult you can create and return a TempPhysicalFileResult, eg :

return new TempPhysicalFileResult(file, "application/zip"){FileDownloadName=fileName};

同样的事情 PhysicalFile() 会:

[NonAction]
public virtual PhysicalFileResult PhysicalFile(
    string physicalPath,
    string contentType,
    string fileDownloadName)
    => new PhysicalFileResult(physicalPath, contentType) { FileDownloadName = fileDownloadName };

更复杂的解决方案是创建一个自定义执行器,该执行器负责压缩和清理文件等工作,让操作代码清除结果格式代码

A more sophisticated solution would be to create a custom executor that took care eg of compression as well as cleaning up files, leaving the action code clean of result formatting code

这篇关于如何在 ASP.NET Core 中流式传输文件后删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)