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

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

      1. <tfoot id='IjNHE'></tfoot><legend id='IjNHE'><style id='IjNHE'><dir id='IjNHE'><q id='IjNHE'></q></dir></style></legend>

      2. 从动作返回文件时,流会被释放吗?

        Does a Stream get Disposed when returning a File from an Action?(从动作返回文件时,流会被释放吗?)

      3. <small id='BOvSA'></small><noframes id='BOvSA'>

          <tfoot id='BOvSA'></tfoot>
            <legend id='BOvSA'><style id='BOvSA'><dir id='BOvSA'><q id='BOvSA'></q></dir></style></legend>
              <tbody id='BOvSA'></tbody>

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

                  <bdo id='BOvSA'></bdo><ul id='BOvSA'></ul>
                  本文介绍了从动作返回文件时,流会被释放吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在向 MemoryStream 写入一个字符串,我需要将流返回到控制器操作,以便我可以将其作为文件发送出去以供下载.

                  I'm writing a string to a MemoryStream I need to return the stream to the Controller Action so I can send it off as a file for download.

                  通常,我将 Stream 包装在 using 语句中,但在这种情况下,我需要返回它.我退货后它仍然会被丢弃吗?还是我需要以某种方式自己处理它?

                  Normally, I wrap the Stream in a using statement, but, in this case, I need to return it. Does it still get Disposed after I return it? Or do I need to dispose it myself somehow?

                  //inside CsvOutputFormatter
                  public Stream GetStream(object genericObject)
                  {
                      var stream = new MemoryStream();
                      var writer = new StreamWriter(stream, Encoding.UTF8);
                      writer.Write(_stringWriter.ToString());
                      writer.Flush();
                      stream.Position = 0;
                      return stream;
                  }
                  

                  返回文件的控制器动作:

                  Controller Action that returns the file:

                  [HttpGet]
                  [Route("/Discussion/Export")]
                  public IActionResult GetDataAsCsv()
                  {
                      var forums = _discussionService.GetForums(_userHelper.UserId);
                  
                      var csvFormatter = new CsvOutputFormatter(new CsvFormatterOptions());
                  
                      var stream = csvFormatter.GetStream(forums);
                      return File(stream, "application/octet-stream", "forums.csv");
                  
                      //is the stream Disposed here automatically?
                  }
                  

                  推荐答案

                  这里根据源码 aspnet/AspNetWebStack/blob/master/src/System.Web.Mvc/FileStreamResult.cs

                  protected override void WriteFile(HttpResponseBase response)
                  {
                      // grab chunks of data and write to the output stream
                      Stream outputStream = response.OutputStream;
                      using (FileStream)
                      {
                          byte[] buffer = new byte[BufferSize];
                  
                          while (true)
                          {
                              int bytesRead = FileStream.Read(buffer, 0, BufferSize);
                              if (bytesRead == 0)
                              {
                                  // no more data
                                  break;
                              }
                  
                              outputStream.Write(buffer, 0, bytesRead);
                          }
                      }
                  }
                  

                  FileStream 将是您调用时传递的流

                  Where FileStream would have been the stream passed when you called

                  return File(stream, "application/octet-stream", "forums.csv");
                  

                  更新.

                  您的问题最初被标记为 Asp.Net MVC,但代码看起来像是更新的核心框架.

                  Your question was originally tagged as Asp.Net MVC but the code looks like the more recent core framework.

                  在那里也找到了它,虽然写法不同,但它在技术上做同样的事情.

                  Found it there as well though written differently it does the same thing technically.

                  aspnet/AspNetCore/blob/master/src/Mvc/Mvc.Core/src/Infrastructure/FileResultExecutorBase.cs

                  protected static async Task WriteFileAsync(HttpContext context, Stream fileStream, RangeItemHeaderValue range, long rangeLength)
                  {
                      var outputStream = context.Response.Body;
                      using (fileStream)
                      {
                          try
                          {
                              if (range == null)
                              {
                                  await StreamCopyOperation.CopyToAsync(fileStream, outputStream, count: null, bufferSize: BufferSize, cancel: context.RequestAborted);
                              }
                              else
                              {
                                  fileStream.Seek(range.From.Value, SeekOrigin.Begin);
                                  await StreamCopyOperation.CopyToAsync(fileStream, outputStream, rangeLength, BufferSize, context.RequestAborted);
                              }
                          }
                          catch (OperationCanceledException)
                          {
                              // Don't throw this exception, it's most likely caused by the client disconnecting.
                              // However, if it was cancelled for any other reason we need to prevent empty responses.
                              context.Abort();
                          }
                      }
                  }
                  

                  这篇关于从动作返回文件时,流会被释放吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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# - 按属性获取所有枚举值)

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

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

                          <legend id='EcmWv'><style id='EcmWv'><dir id='EcmWv'><q id='EcmWv'></q></dir></style></legend>
                          • <bdo id='EcmWv'></bdo><ul id='EcmWv'></ul>