简体   繁体   English

MP4 内容类型在不同浏览器中返回为不同的 MIME 类型

[英]MP4 content type returned as different MIME type in different browsers

I have an ASP.NET application built using C# for the backend.我有一个使用 C# 作为后端构建的 ASP.NET 应用程序。 I have a form that uploads a pdf and checks the MIME type, application/pdf, to verify it's a valid file.我有一个上传 pdf 并检查 MIME 类型 application/pdf 以验证它是有效文件的表单。 I need to do this on a seperate form for MP4 files but it didn't seem to work.我需要在 MP4 文件的单独表单上执行此操作,但它似乎不起作用。 It always returned false.它总是返回假。 I checked the returned MIME type FileUpload.PostedFile.ContentType in webkit which was perfectly accurate.我检查了 webkit 中返回的 MIME 类型 FileUpload.PostedFile.ContentType,它非常准确。 Firefox 5 and IE 8, however, tested for text/csv and application/octet-stream, respectively.然而,Firefox 5 和 IE 8 分别针对 text/csv 和 application/octet-stream 进行了测试。 This makes absolutely no sense to me.这对我来说完全没有意义。 I also tried mapping extensions to the correct MIME types in the web.config file like so:我还尝试将扩展名映射到 web.config 文件中的正确 MIME 类型,如下所示:

<staticContent>
  <mimeMap fileExtension=".mp4" mimeType="video/mp4" />
  <mimeMap fileExtension=".m4v" mimeType="video/m4v" />
  <mimeMap fileExtension=".ogg" mimeType="video/ogg" />
  <mimeMap fileExtension=".ogv" mimeType="video/ogg" />
  <mimeMap fileExtension=".webm" mimeType="video/webm" />
</staticContent>

And in the backend CS file I used this to test the posted file's content type:在后端 CS 文件中,我使用它来测试发布文件的内容类型:

if (file.PostedFile.ContentType == "video/mp4" || 
    file.PostedFile.ContentType == "video/mpeg" || 
    file.PostedFile.ContentType == "video/ogg" || 
    file.PostedFile.ContentType == "video/quicktime" || 
    file.PostedFile.ContentType == "video/webm") 
        return true;
else
        return false;

Also please note I am using the local development server that comes with VS另请注意,我使用的是 VS 自带的本地开发服务器

It is the browser on the client side that determines the content type that gets sent to the server .确定发送到服务器的内容类型的是客户端的浏览器 That is the browser sends the header that ASP.NET uses to fill the HttpPostedFile.ContentType property.也就是说,浏览器发送 ASP.NET 用来填充 HttpPostedFile.ContentType 属性的 header。

You can't change this with IIS staticContent mime mappings , those settings apply only to files sent by the server to the client (not the other way around).您无法使用 IIS staticContent mime mappings 更改此设置,这些设置仅适用于服务器发送到客户端的文件(而不是相反)。

You'd better use some custom code in your c# method before the if-else block you posted, to (attempt to) determine the real mime type with a server side check .在您发布 if-else 块之前,您最好在 c# 方法中使用一些自定义代码,以(尝试)通过服务器端检查确定真正的 mime 类型。 Take a look at those two methods, that lead to different results and performance:看看这两种方法,它们会导致不同的结果和性能:

  1. Use reflection to call to a method in a internal .NET framework class (this simply matches the file extension with those known to the framework itself) 使用反射调用内部.NET 框架 class 中的方法(这只是将文件扩展名与框架本身已知的文件扩展名匹配)
  2. Use an un-managed call to a IE dll (this method should guess the mime type looking for magic bytes inside the file, as you can read in the MSDN documentation ) 使用对 IE dll 的非托管调用(此方法应该猜测 mime 类型,在文件中查找魔术字节如您可以在 MSDN 文档中阅读的那样

Note: generally speaking you should always check/determine the content type on the server and never rely on the one sent by the client (never trust HttpPostedFile.ContentType): it can be accidentally wrong (as happened to you), purposely changed by some hacky user, wrong because of a browser not complying to some standard (IE is known to send wrong mime types for some image formats)...注意:一般来说,您应该始终检查/确定服务器上的内容类型,并且永远不要依赖客户端发送的内容类型(永远不要相信 HttpPostedFile.ContentType):它可能是意外错误(就像您发生的那样),被某些人故意更改hacky 用户,错误是因为浏览器不符合某些标准(众所周知,IE 会为某些图像格式发送错误的 mime 类型)...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM