asp.net-mvc ASP.NET MVC 中的四个文件结果有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1187261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:44:14  来源:igfitidea点击:

What's the difference between the four File Results in ASP.NET MVC

asp.net-mvcfileresulttype

提问by Robert MacLean

ASP.NET has four different types of file results:

ASP.NET 有四种不同类型的文件结果:

  • FileContentResult: Sends the contents of a binary file to the response.
  • FilePathResult: Sends the contents of a file to the response
  • FileResult: Returns binary output to write to the response
  • FileStreamResult: Sends binary content to the response by using a Stream instance
  • FileContentResult:将二进制文件的内容发送到响应。
  • FilePathResult:将文件内容发送到响应
  • FileResult:返回二进制输出以写入响应
  • FileStreamResult:使用 Stream 实例将二进制内容发送到响应

Those descriptions are take from MSDN and with the exception of the FileStreamResult the first three sound identical. So what is the difference between them?

这些描述取自 MSDN,除了 FileStreamResult 之外,前三个听起来相同。那么它们之间有什么区别呢?

回答by maciejkow

FileResultis an abstract base class for all the others.

FileResult是所有其他人的抽象基类。

  • FileContentResult- you use it when you have a byte array you would like to return as a file
  • FilePathResult- when you have a file on disk and would like to return its content (you give a path)
  • FileStreamResult- you have a stream open, you want to return its content as a file
  • FileContentResult- 当你有一个想要作为文件返回的字节数组时使用它
  • FilePathResult- 当您在磁盘上有一个文件并想返回其内容时(您提供路径)
  • FileStreamResult- 您打开了一个流,您想将其内容作为文件返回

However, you'll rarely have to use these classes - you can just use one of Controller.Fileoverloads and let ASP.NET MVC do the magic for you.

但是,您很少需要使用这些类 - 您可以只使用Controller.File重载之一并让 ASP.NET MVC 为您做魔术。

回答by beauXjames

Great question...and deserves more details. I find myself here as a result of an interesting situation. We were delivering some pdf attachments via the MVC3/C# environment. Our code got released and we started getting some responses from our clients that the downloads were behaving strangely when they were using Chrome and the file type was being converted over to 'pdf-, attachment.pdf-, attachment'. Yup...you got it...the whole thing. So, one could rewrite it to just be 'pdf' and the file would still save intact, but what a mess!

好问题……值得更多细节。由于一个有趣的情况,我发现自己在这里。我们通过 MVC3/C# 环境提供了一些 pdf 附件。我们的代码发布了,我们开始从客户那里得到一些回应,说他们在使用 Chrome 时下载的行为很奇怪,并且文件类型被转换为“pdf-,attachment.pdf-,attachment”。是的......你明白了......整个事情。因此,您可以将其重写为“pdf”,并且该文件仍会完整保存,但真是一团糟!

So, to describe the initial situation, we were setting the 'Content-Disposition' header then returning a FileContentResult...

因此,为了描述初始情况,我们设置了“Content-Disposition”标头,然后返回一个 FileContentResult...

var cd = new System.Net.Mime.ContentDisposition
            {
                FileName = result.Attachment.FileName,
                Inline = false
            };
            Response.AppendHeader("Content-Disposition", cd.ToString());

return File(result.Attachment.Data, MimeExtensionHelper.GetMimeType(result.Attachment.FileName), result.Attachment.FileName);

Seemed good. Worked fine in IE. So I did some research and tried implementing FileStreamResult instead (keeping the Content-Disposition setter):

看起来不错。在 IE 中运行良好。所以我做了一些研究并尝试实现 FileStreamResult (保留 Content-Disposition setter):

MemoryStream dataStream = new MemoryStream();
dataStream.Write(result.Attachment.Data, 0, result.Attachment.Data.Length);
dataStream.Position = 0;
return new FileStreamResult(dataStream, MimeExtensionHelper.GetMimeType(result.Attachment.FileName));

It fixed the issue in Chrome! Hmmm...but why in the heck should I have to take my perfectly good byte array and stream it and then return it via this to get the file name to work right?

它修复了 Chrome 中的问题!嗯......但是为什么我必须使用我完美的字节数组并流式传输它然后通过它返回它以使文件名正常工作?

Then came the Fiddler.

然后是提琴手。

With FileContentResult, I got 2 Content-Dispositions in the header. With FileStreamResult, I got 1.

使用 FileContentResult,我在标题中有 2 个 Content-Dispositions。使用 FileStreamResult,我得到了 1。

FileContentResult appends a Content-Disposition header when providing the File Name and Chrome considers multiples of this header as an error.

FileContentResult 在提供文件名时附加一个 Content-Disposition 标头,Chrome 将此标头的倍数视为错误。

Odd reaction...but definitely one that's good to know.

奇怪的反应......但绝对是一个很好的了解。