中流式传输文件后删除文件

中流式传输文件后删除文件

本文介绍了如何在 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 派生类,它只是 委托处理给一个执行服务.PhysicalFileResultExecuteResultAsync 方法调用:

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 中流式传输文件后删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 19:01