<tfoot id='CjKiB'></tfoot>

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

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

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

        在 ASP.NET MVC 中返回文件以查看/下载

        Returning a file to View/Download in ASP.NET MVC(在 ASP.NET MVC 中返回文件以查看/下载)
        <i id='cBG18'><tr id='cBG18'><dt id='cBG18'><q id='cBG18'><span id='cBG18'><b id='cBG18'><form id='cBG18'><ins id='cBG18'></ins><ul id='cBG18'></ul><sub id='cBG18'></sub></form><legend id='cBG18'></legend><bdo id='cBG18'><pre id='cBG18'><center id='cBG18'></center></pre></bdo></b><th id='cBG18'></th></span></q></dt></tr></i><div id='cBG18'><tfoot id='cBG18'></tfoot><dl id='cBG18'><fieldset id='cBG18'></fieldset></dl></div>
        • <bdo id='cBG18'></bdo><ul id='cBG18'></ul>
        • <tfoot id='cBG18'></tfoot>

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

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

                  <tbody id='cBG18'></tbody>

                  本文介绍了在 ASP.NET MVC 中返回文件以查看/下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在将存储在数据库中的文件发送回 ASP.NET MVC 中的用户时遇到问题.我想要的是一个列出两个链接的视图,一个用于查看文件并让发送到浏览器的 mimetype 确定应如何处理,另一个用于强制下载.

                  I'm encountering a problem sending files stored in a database back to the user in ASP.NET MVC. What I want is a view listing two links, one to view the file and let the mimetype sent to the browser determine how it should be handled, and the other to force a download.

                  如果我选择查看一个名为 SomeRandomFile.bak 的文件并且浏览器没有关联的程序来打开这种类型的文件,那么默认为下载行为我没有问题.但是,如果我选择查看名为 SomeRandomFile.pdfSomeRandomFile.jpg 的文件,我希望文件能够简单地打开.但我也想保留一个下载链接,以便无论文件类型如何,我都可以强制下载提示.这有意义吗?

                  If I choose to view a file called SomeRandomFile.bak and the browser doesn't have an associated program to open files of this type, then I have no problem with it defaulting to the download behavior. However, if I choose to view a file called SomeRandomFile.pdf or SomeRandomFile.jpg I want the file to simply open. But I also want to keep a download link off to the side so that I can force a download prompt regardless of the file type. Does this make sense?

                  我已经尝试过 FileStreamResult 并且它适用于大多数文件,它的构造函数默认情况下不接受文件名,因此根据 URL 为未知文件分配一个文件名(它不知道根据内容类型给出的扩展名).如果我通过指定文件名来强制使用它,我将失去浏览器直接打开文件的能力,并且会收到下载提示.有没有其他人遇到过这种情况?

                  I have tried FileStreamResult and it works for most files, its constructor doesn't accept a filename by default, so unknown files are assigned a file name based on the URL (which does not know the extension to give based on content type). If I force the file name by specifying it, I lose the ability for the browser to open the file directly and I get a download prompt. Has anyone else encountered this?

                  这些是我迄今为止尝试过的示例.

                  These are the examples of what I've tried so far.

                  //Gives me a download prompt.
                  return File(document.Data, document.ContentType, document.Name);
                  

                  //Opens if it is a known extension type, downloads otherwise (download has bogus name and missing extension)
                  return new FileStreamResult(new MemoryStream(document.Data), document.ContentType);
                  

                  //Gives me a download prompt (lose the ability to open by default if known type)
                  return new FileStreamResult(new MemoryStream(document.Data), document.ContentType) {FileDownloadName = document.Name};
                  

                  有什么建议吗?

                  更新:这个问题似乎引起了很多人的共鸣,所以我想我会发布一个更新.Oskar 添加的关于国际字符的以下已接受答案的警告是完全有效的,由于使用了 ContentDisposition 类,我已经打了几次.我已经更新了我的实现来解决这个问题.虽然下面的代码来自我最近在 ASP.NET Core(完整框架)应用程序中出现的这个问题,但由于我使用的是 System.Net.Http.Headers.ContentDispositionHeaderValue 类.

                  UPDATE: This questions seems to strike a chord with a lot of people, so I thought I'd post an update. The warning on the accepted answer below that was added by Oskar regarding international characters is completely valid, and I've hit it a few times due to using the ContentDisposition class. I've since updated my implementation to fix this. While the code below is from my most recent incarnation of this problem in an ASP.NET Core (Full Framework) app, it should work with minimal changes in an older MVC application as well since I'm using the System.Net.Http.Headers.ContentDispositionHeaderValue class.

                  using System.Net.Http.Headers;
                  
                  public IActionResult Download()
                  {
                      Document document = ... //Obtain document from database context
                  
                      //"attachment" means always prompt the user to download
                      //"inline" means let the browser try and handle it
                      var cd = new ContentDispositionHeaderValue("attachment")
                      {
                          FileNameStar = document.FileName
                      };
                      Response.Headers.Add(HeaderNames.ContentDisposition, cd.ToString());
                  
                      return File(document.Data, document.ContentType);
                  }
                  
                  // an entity class for the document in my database 
                  public class Document
                  {
                      public string FileName { get; set; }
                      public string ContentType { get; set; }
                      public byte[] Data { get; set; }
                      //Other properties left out for brevity
                  }
                  

                  推荐答案

                  public ActionResult Download()
                  {
                      var document = ...
                      var cd = new System.Net.Mime.ContentDisposition
                      {
                          // for example foo.bak
                          FileName = document.FileName, 
                  
                          // always prompt the user for downloading, set to true if you want 
                          // the browser to try to show the file inline
                          Inline = false, 
                      };
                      Response.AppendHeader("Content-Disposition", cd.ToString());
                      return File(document.Data, document.ContentType);
                  }
                  

                  注意: 上面的示例代码未能正确考虑文件名中的国际字符.相关标准化见 RFC6266.我相信 ASP.Net MVC 的 File() 方法和 ContentDispositionHeaderValue 类的最新版本正确地解释了这一点.- 奥斯卡 2016-02-25

                  NOTE: This example code above fails to properly account for international characters in the filename. See RFC6266 for the relevant standardization. I believe recent versions of ASP.Net MVC's File() method and the ContentDispositionHeaderValue class properly accounts for this. - Oskar 2016-02-25

                  这篇关于在 ASP.NET MVC 中返回文件以查看/下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Adding and removing users from Active Directory groups in .NET(在 .NET 中的 Active Directory 组中添加和删除用户)
                  set equality in linq(在 linq 中设置相等)
                  HashSet conversion to List(HashSet 转换为 List)
                  How to set timeout for webBrowser navigate event(如何为 webBrowser 导航事件设置超时)
                  Test whether two IEnumerablelt;Tgt; have the same values with the same frequencies(测试两个IEnumerablelt;Tgt;具有相同频率的相同值)
                  How do you determine if two HashSets are equal (by value, not by reference)?(您如何确定两个 HashSet 是否相等(按值,而不是按引用)?)

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

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

                              <tbody id='FaWON'></tbody>
                          1. <legend id='FaWON'><style id='FaWON'><dir id='FaWON'><q id='FaWON'></q></dir></style></legend>