在不同的浏览器中以不同的MIME类型返回的MP4内容类型 [英] MP4 content type returned as different MIME type in different browsers

查看:136
本文介绍了在不同的浏览器中以不同的MIME类型返回的MP4内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用C#后端构建的ASP.NET应用程序.我有一个上载pdf并检查MIME类型application/pdf的表单,以确认它是有效文件.我需要为MP4文件使用单独的格式执行此操作,但是它似乎没有用.它总是返回false.我在webkit中检查了返回的MIME类型FileUpload.PostedFile.ContentType,这是非常准确的.但是,Firefox 5和IE 8分别测试了text/csv和application/octet-stream.这对我来说绝对没有意义.我还尝试将扩展名映射到web.config文件中的正确MIME类型,如下所示:

I have an ASP.NET application built using C# for the backend. I have a form that uploads a pdf and checks the MIME type, application/pdf, to verify it's a valid file. I need to do this on a seperate form for MP4 files but it didn't seem to work. It always returned false. I checked the returned MIME type FileUpload.PostedFile.ContentType in webkit which was perfectly accurate. Firefox 5 and IE 8, however, tested for text/csv and application/octet-stream, respectively. This makes absolutely no sense to me. I also tried mapping extensions to the correct MIME types in the web.config file like so:

<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>

在后端CS文件中,我使用它来测试发布文件的内容类型:

And in the backend CS file I used this to test the posted file's content type:

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;

还请注意,我正在使用VS随附的本地开发服务器

Also please note I am using the local development server that comes with VS

推荐答案

是客户端的浏览器,用于确定发送的内容类型到服务器.那就是浏览器发送ASP.NET用来填充HttpPostedFile.ContentType属性的标头.

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.

您无法使用IIS staticContent mime映射来更改此设置,这些设置仅适用于服务器发送给客户端的文件(反之则不适用).

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).

您最好在发布的if-else块之前,在c#方法中使用一些自定义代码,以(尝试)确定真实的 mime输入服务器端检查. 看一下这两种导致不同结果和性能的方法:

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. Take a look at those two methods, that lead to different results and performance:

  1. 使用不受管理的调用来IE dll (此方法应猜测在文件
  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)
  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)

注意:一般来说,您应该始终检查/确定服务器上的内容类型,并且永远不要依赖客户端发送的内容类型(永远不要信任HttpPostedFile.ContentType):可能是偶然的错误(就像您发生的那样) ),是由一些骇客用户故意更改的,因为浏览器不符合某些标准而导致错误(已知IE会针对某些图像格式发送错误的mime类型)...

这篇关于在不同的浏览器中以不同的MIME类型返回的MP4内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆