Search code examples
c#asp.netflashmime-typesuploadify

MimeType is always application/octet-stream


I am having a problem that in my ashx.cs handler I always get a content type of application/octet-stream even when I upload images.

I use uploadify to do the upload, and to begin with I was using uploadify v2.1.0. I have since upgraded to uploadify 3.1. Regardless, I receive application/octet-stream as the ContentType using either version.

I read that it may be a flash player problem, so I un-installed flash using their uninstaller, and tried both Flash Player 10.1.102.64, and 11_1r102_55_64bit and tried re-installing the latest version again. All three versions didn't change the content type.

I have used Internet Explorer 8 and 9, without any change.. and Windows Server 2008 R2 64bit and Windows 7 64bit.

My .ashx handler:

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

I am out of ideas at this point... this was working previously until something changed, and am now trying to figure out what...


Solution

  • Well, this is not an asp.net isue at all.

    The mime type is part of the HTTP protocol sequence sent by the browser and it is his responsibility to determine it.

    Some suck, some are ok, sometimes.

    Check

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

    for an analysis what mime types get transferred for a cs upload. Quite revealing.

    If you need to identify what the file is, you have to do that server side, assuming the file extension is correct and / or parsing the file. I remember when we wrote a CMS 10 years ago, we actually loaded all images uploaded into memory to make sure they are valid. You can not rely on the mime type to be proper. But you do get a file name, which can be used to find the "correct" mime type, unless the file has the wrong extension.