简体   繁体   English

为什么我需要 Content/Type 变量?

[英]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.

相关问题 为什么在使用Linq时需要初始化out变量 - Why do I need to initialize an out variable when using Linq 为什么我需要初始化字符串变量'name'? - Why do I need to initialize the string variable 'name'? 为什么我需要在泛型子类中重新声明类型约束 - Why Do I need to redeclare type constraint in generic subclass WPF依赖属性:为什么我需要指定所有者类型? - WPF Dependency Properties: Why do I need to specify an Owner Type? 为什么我得到不同的过帐文件内容类型? - Why do I get different posted file content type? 为什么在Azure上设置ASPNETCORE_ENVIRONMENT变量时需要发布我的project.json - Why do I need to publish my project.json when setting ASPNETCORE_ENVIRONMENT variable on Azure 如果我的“对象”类型的对象指向层次结构中的正确实例,为什么我需要显式下调? - Why do I need an explicit downcast if my object of type “object” is pointing to the correct instance in hierarchy? 为什么需要在C#显式实现中将'this'转换为接口类型? - Why do I need to cast 'this' to interface type in a C# explicit implementation? 在引用枚举类型时,为什么需要在枚举的声明中添加名称空间和类? - in referencing enum type why do i need to add namespace and class in declaration of the enum? 为什么需要在泛型类上显式定义泛型参数的泛型类型? - Why do I need to explicitly define the generic argument's generic type on a generic class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM