• <small id='3OcFD'></small><noframes id='3OcFD'>

    <tfoot id='3OcFD'></tfoot>
        <bdo id='3OcFD'></bdo><ul id='3OcFD'></ul>
      <i id='3OcFD'><tr id='3OcFD'><dt id='3OcFD'><q id='3OcFD'><span id='3OcFD'><b id='3OcFD'><form id='3OcFD'><ins id='3OcFD'></ins><ul id='3OcFD'></ul><sub id='3OcFD'></sub></form><legend id='3OcFD'></legend><bdo id='3OcFD'><pre id='3OcFD'><center id='3OcFD'></center></pre></bdo></b><th id='3OcFD'></th></span></q></dt></tr></i><div id='3OcFD'><tfoot id='3OcFD'></tfoot><dl id='3OcFD'><fieldset id='3OcFD'></fieldset></dl></div>

        <legend id='3OcFD'><style id='3OcFD'><dir id='3OcFD'><q id='3OcFD'></q></dir></style></legend>

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

        How to delete a file after it was streamed in ASP.NET Core(如何在 ASP.NET Core 中流式传输文件后删除文件)
      1. <legend id='z8hVK'><style id='z8hVK'><dir id='z8hVK'><q id='z8hVK'></q></dir></style></legend>
        <tfoot id='z8hVK'></tfoot>

        1. <i id='z8hVK'><tr id='z8hVK'><dt id='z8hVK'><q id='z8hVK'><span id='z8hVK'><b id='z8hVK'><form id='z8hVK'><ins id='z8hVK'></ins><ul id='z8hVK'></ul><sub id='z8hVK'></sub></form><legend id='z8hVK'></legend><bdo id='z8hVK'><pre id='z8hVK'><center id='z8hVK'></center></pre></bdo></b><th id='z8hVK'></th></span></q></dt></tr></i><div id='z8hVK'><tfoot id='z8hVK'></tfoot><dl id='z8hVK'><fieldset id='z8hVK'></fieldset></dl></div>

          <small id='z8hVK'></small><noframes id='z8hVK'>

            <tbody id='z8hVK'></tbody>
              • <bdo id='z8hVK'></bdo><ul id='z8hVK'></ul>
                  本文介绍了如何在 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 中流式传输文件后删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  C# Attributes and their uses(C# 属性及其用途)
                  C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)
                    <tbody id='6tBmi'></tbody>

                      • <legend id='6tBmi'><style id='6tBmi'><dir id='6tBmi'><q id='6tBmi'></q></dir></style></legend>
                      • <small id='6tBmi'></small><noframes id='6tBmi'>

                        <i id='6tBmi'><tr id='6tBmi'><dt id='6tBmi'><q id='6tBmi'><span id='6tBmi'><b id='6tBmi'><form id='6tBmi'><ins id='6tBmi'></ins><ul id='6tBmi'></ul><sub id='6tBmi'></sub></form><legend id='6tBmi'></legend><bdo id='6tBmi'><pre id='6tBmi'><center id='6tBmi'></center></pre></bdo></b><th id='6tBmi'></th></span></q></dt></tr></i><div id='6tBmi'><tfoot id='6tBmi'></tfoot><dl id='6tBmi'><fieldset id='6tBmi'></fieldset></dl></div>

                        • <bdo id='6tBmi'></bdo><ul id='6tBmi'></ul>
                          <tfoot id='6tBmi'></tfoot>