【问题标题】:MimeType is always application/octet-streamMimeType 始终是 application/octet-stream
【发布时间】:2012-12-04 12:00:58
【问题描述】:

我遇到了一个问题,即在我的 ashx.cs 处理程序中,即使我上传图片,我总是得到 application/octet-stream 的内容类型。

我使用uploadify 进行上传,一开始我使用的是uploadify v2.1.0。我已经升级到uploadify 3.1。无论如何,我使用任一版本都会收到 application/octet-stream 作为 ContentType。

我了解到这可能是 Flash 播放器的问题,所以我使用 their uninstaller 卸载了 Flash,并尝试了 Flash Player 10.1.102.6411_1r102_55_64bit,并尝试再次重新安装最新版本。所有三个版本都没有改变内容类型。

我使用过 Internet Explorer 8 和 9,没有任何变化。以及 Windows Server 2008 R2 64 位和 Windows 7 64 位。

我的.ashx 处理程序:

namespace HttpHandlers
{
    public class UploadsHandler
        : IHttpHandler
    {
        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler" /> interface.
        /// </summary>
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                HttpPostedFile file = context.Request.Files["Filedata"];
                string mimeType = file.ContentType; // always application/octet-stream

                context.Response.ContentType = "text/plain";
                context.Response.Write("success");
            }
            catch (Exception ex)
            {
                context.Response.ContentType = "text/plain";
                context.Response.Write(ex.Message);
            }
        }

        /// <summary>
        /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler" /> instance.
        /// </summary>
        /// <returns>true if the <see cref="T:System.Web.IHttpHandler" /> instance is reusable; otherwise, false.</returns>
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

此时我没有想法...这在之前一直有效,直到 某些东西发生了变化,现在我正试图弄清楚是什么...

【问题讨论】:

  • 可能是服务器 MIME 类型配置问题...

标签: c# asp.net flash mime-types uploadify


【解决方案1】:

嗯,这根本不是 asp.net 问题。

mime 类型是浏览器发送的 HTTP 协议序列的一部分,由浏览器负责确定。

有些糟糕,有些还可以,有时。

检查

http://blog.futtta.be/2009/08/24/http-upload-mime-type-hell/

分析哪些 mime 类型会为 cs 上传而传输。很有启发性。

如果您需要识别文件是什么,则必须在服务器端进行,假设文件扩展名正确和/或解析文件。我记得 10 年前我们写一个 CMS 时,我们实际上将所有上传到内存中的图像加载到内存中以确保它们是有效的。您不能依赖 mime 类型是正确的。但是你确实得到了一个文件名,它可以用来找到“正确”的 mime 类型,除非文件的扩展名错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-08
    • 2018-10-05
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    相关资源
    最近更新 更多