How can I get the content/mime type?

Last post 11-16-2012, 8:16 PM by Adam. 8 replies.
Sort Posts: Previous Next
  •  07-11-2009, 9:25 AM 53907

    How can I get the content/mime type?

    I have read the posts about why you don't provide contenttype, and how the developer should rely on filename extensions, however, some of my users will be uploading a variety of files from Macs. I can't rely on them having file extensions.
     
    Is there away I can derive the contenttype from their uploaded files (other than checking extensions)?
     
    Thanks,
     Andy
    Filed under: , , ,
  •  07-11-2009, 11:20 PM 53912 in reply to 53907

    Re: How can I get the content/mime type?

    Hi,
     
    We are considering this function.
     
    Regards,
    Terry
     
  •  10-14-2009, 3:51 AM 56386 in reply to 53912

    Re: How can I get the content/mime type?

    Hi,
    I too need to know the mime type of uploaded files. Do you have any update about this?
    Thanks in advanced
    Pierre
  •  10-15-2009, 8:36 AM 56416 in reply to 56386

    Re: How can I get the content/mime type?

    Found this link on Stackoverflow if you want to implement this yourself.
    Look at the second answer for a solution to detecting mime types of files that have been uploaded and renamed.
  •  10-15-2009, 8:51 AM 56418 in reply to 56416

    Re: How can I get the content/mime type?

    When using AjaxUploader, you don't have a HttpPostedFile object... So this solution does not help...
  •  10-15-2009, 9:04 AM 56419 in reply to 56418

    Re: How can I get the content/mime type?

    1. [DllImport(@"urlmon.dll", CharSet = CharSet.Auto)]   
    2.     private extern static System.UInt32 FindMimeFromData(   
    3.         System.UInt32 pBC,   
    4.         [MarshalAs(UnmanagedType.LPStr)] System.String pwzUrl,   
    5.         [MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer,   
    6.         System.UInt32 cbSize,   
    7.         [MarshalAs(UnmanagedType.LPStr)] System.String pwzMimeProposed,   
    8.         System.UInt32 dwMimeFlags,   
    9.         out System.UInt32 ppwzMimeOut,   
    10.         System.UInt32 dwReserverd   
    11.     );   
    12.   
    13.     public string getMimeFromFile(string filename)   
    14.     {   
    15.         if (!File.Exists(filename))   
    16.             throw new FileNotFoundException(filename + " not found");   
    17.   
    18.         byte[] buffer = new byte[256];   
    19.         using (FileStream fs = new FileStream(filename, FileMode.Open))   
    20.         {   
    21.             if (fs.Length >= 256)   
    22.                 fs.Read(buffer, 0, 256);   
    23.             else  
    24.                 fs.Read(buffer, 0, (int)fs.Length);   
    25.         }   
    26.         try  
    27.         {   
    28.             System.UInt32 mimetype;   
    29.             FindMimeFromData(0, null, buffer, 256, null, 0, out mimetype, 0);   
    30.             System.IntPtr mimeTypePtr = new IntPtr(mimetype);   
    31.             string mime = Marshal.PtrToStringUni(mimeTypePtr);   
    32.             Marshal.FreeCoTaskMem(mimeTypePtr);   
    33.             return mime;   
    34.         }   
    35.         catch (Exception e)   
    36.         {   
    37.             return "unknown/unknown";   
    38.         }   
    39.     }  

     

    Give that a shot

  •  10-17-2009, 11:16 PM 56465 in reply to 56419

    Re: How can I get the content/mime type?

    Hi,
     
    That is a very cool solution.
     
    We will consider add it to the uploader , and consider 3 methods :
     
    1 - GetMimeTypeFromBrowser()
    2 - GetMimeTypeFromFileExtension()
    3 - GetMimeTypeFromFileData()
     
    and will add security warning to that methods.
     
    Thanks.
     
    Regards,
    Terry
  •  10-19-2009, 3:03 AM 56475 in reply to 56465

    Re: How can I get the content/mime type?

    This is a good news!
    Thanks for letting us when the new dll is available for download.
    Regards,
    Pierre
  •  11-16-2012, 8:16 PM 75276 in reply to 56475

    Re: How can I get the content/mime type?

    pierre:
    This is a good news!
    Thanks for letting us when the new dll is available for download.
    Regards,
    Pierre

     

    This function is implemented in the latest build.

     

    Article:

    Enable and disable mimetype checking on uploads

     

    Online Demo:

    Enabling and disabling mimetype checking on uploads

     

    Download:

    Ajax Uploader for ASP.NET 


    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

View as RSS news feed in XML