[英]Why do I need Content/Type variable?
In my MVC Core application, I have in my controller a way for the user to download an image that I have saved which is called from a button click in my view.在我的 MVC Core 应用程序中,我在 controller 中有一种方法供用户下载我保存的图像,该图像通过在我的视图中单击按钮调用。 I don't understand why I need the second variable "contentType" string in my return File().
我不明白为什么我的 return File() 中需要第二个变量“contentType”字符串。 I can still download the image as a.jpg if I put in "application/pdf" instead of "application/jpg", so what is even the point of having it other than the fact that it is required.
如果我输入“application/pdf”而不是“application/jpg”,我仍然可以将图像下载为 a.jpg,所以除了它是必需的事实之外,拥有它还有什么意义。 What is the point of this variable?
这个变量有什么意义?
public FileStreamResult Download()
{
string filename = "Capture.JPG";
string filepath = hostingEnvironment.WebRootPath + "\\Image";
string fullName = Path.Combine(filepath, filename);
return File(new FileStream(fullName, FileMode.Open), "application/pdf", filename);
}
Flydog's comment is quite pertinent; Flydog 的评论很中肯; perhaps the most useful or obvious difference you'll find in declaring a content type is that the browser may use it to invoke a different behavior depending on the setting.
也许您在声明内容类型时会发现最有用或最明显的区别是浏览器可能会根据设置使用它来调用不同的行为。 If you send an mp4 video as "video/mp4" the browser may well play it within its viewport.
如果您将 mp4 视频作为“video/mp4”发送,浏览器可能会在其视口内播放它。 If you send the same file as "application/octet-stream" it may offer a save as.
如果您发送与“application/octet-stream”相同的文件,它可能会提供另存为。 If you claim one content type and it's actually another it's likely the browser will ignore you (note: claiming a pdf is pdf or octet stream are both reasonable/true. Claiming it's a jog is not).
如果您声明一种内容类型而实际上是另一种内容类型,则浏览器很可能会忽略您(注意:声明 pdf 是 pdf 或八位字节 ZF7B44CFAFD5C52223D5498196C8A2E 都是合理的)。 Content-Disposition also plays a part in the server aiming to give the browser some direction on what to do with the file..
Content-Disposition 也在服务器中发挥作用,旨在为浏览器提供有关如何处理文件的方向。
..but primarily I'd say a good reason to tell the truth about the content type (ie don't claim it's a jpg when it's a pdf) is the interop mantra "when it sones to standards adherence, be strict in what you send and liberal in what you accept" - you're sending, so follow protocol:) ..但首先我想说一个很好的理由来讲述有关内容类型的真相(即当它是 pdf 时不要声称它是 jpg)是互操作的口头禅“当它符合标准时,要严格你的发送并自由地接受您接受的内容”-您要发送,因此请遵循协议:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.