Results 1 to 6 of 6

Thread: [RESOLVED] MIME Type / ContentType problem

  1. #1

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Resolved [RESOLVED] MIME Type / ContentType problem

    I have a webpage that has FileUpload control to upload files to server. The FileUpload.ContentType is also saved in database and later used in other procedures.
    Now I need an equivalent functionality in my webservice. I get the files from user in byte array. But how should I determine the content type?

    I tried using the urlmon.dll. But the results for FileUpload.ContentType and the urlmon.dll are different. The FindMimeFromData of urlmon.dll and FileUpload.ContentType return different values for same file.
    e.g. For a .csv file, FindMimeFromData returns "text/plain" while the FileUpload ContentType returns "application/vnd.ms-excel".

    Anyone knows of any way to get the same ContentType that is returned from asp.net FileUpload control?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: MIME Type / ContentType problem

    Hey,

    In all likelihood, the method that is being used by the urlmon will be inspecting the first few bytes of the array, to try to figure out what the type is. In the instance of a CSV file, it has figured out that it is a plain text file, which essentially is what it is. Whereas, the FileUpload control will have looked at the extension, and made the leap that the csv extension is associated with Excel. It is unlikely that you will be able to get the information you want directly from the byte array.

    Is it possible that you can pass this information into the Web Service? i.e. have the user select the extension from a drop down list, or something like that?

    Gary

  3. #3

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: MIME Type / ContentType problem

    I really don't want the end user to intervene in this unless there is no other option, since they can always provide incorrect input. I can automatically pick up the file extension the user is providing to my webservice-client and pass it to my webservice along with the file bytes.

    With a FileUpload control, I know it also considers the file extension when determining the ContentType and can be faked. But that's really not my concern. The concern is if the user is providing same file (same file contents and filename) to my webservice-client and to my regular web application, the FileContent field in my database should be same.

    The FileUpload control sometimes considers file extension while at other times it considers the file contents to determine the value of ContentType. Do you know what is considered when? If I know this, then I can use a combination of the urlmon.dll method and a file extension lookup list to determine the correct value.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: MIME Type / ContentType problem

    Hey,

    On that, I am really not sure. I don't know what the combinations are. Sorry

    Gary

  5. #5

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Resolved Re: MIME Type / ContentType problem

    ok. So I made a class for my self.

    I got clues from here:
    http://msdn.microsoft.com/en-us/library/ms775147.aspx

    You can build your own class from the algorithm given there.

    Here is the code for anyone who might be in a similar situation that I am in.
    (The output is not 100% exact, but is at-least sufficient for what I was looking for)

    vb.net Code:
    1. Option Strict On
    2. Imports System.Runtime.InteropServices
    3. Imports System.IO
    4.  
    5. Public Class MimeTypes
    6.     Private Shared knownTypes As List(Of String)
    7.     Private Shared mimeTypes As Dictionary(Of String, String)
    8.  
    9.     <DllImport("urlmon.dll", CharSet:=CharSet.Auto)> _
    10.     Private Shared Function FindMimeFromData(ByVal pBC As UInt32, <MarshalAs(UnmanagedType.LPStr)> ByVal pwzUrl As String, <MarshalAs(UnmanagedType.LPArray)> ByVal pBuffer As Byte(), ByVal cbSize As UInt32, <MarshalAs(UnmanagedType.LPStr)> ByVal pwzMimeProposed As String, ByVal dwMimeFlags As UInt32, ByRef ppwzMimeOut As UInt32, ByVal dwReserverd As UInt32) As UInt32
    11.     End Function
    12.  
    13.     Public Shared Function GetContentType(ByVal fileName As String) As String
    14.         If knownTypes Is Nothing OrElse mimeTypes Is Nothing Then InitializeMimeTypeLists()
    15.         Dim contentType As String = ""
    16.         Dim extension As String = IO.Path.GetExtension(fileName).Replace(".", "").ToLower
    17.         mimeTypes.TryGetValue(extension, contentType)
    18.         If String.IsNullOrEmpty(contentType) OrElse knownTypes.Contains(contentType) Then
    19.             Dim headerType As String = ScanFileForMimeType(fileName)
    20.             If headerType <> "application/octet-stream" OrElse String.IsNullOrEmpty(contentType) Then contentType = headerType
    21.         End If
    22.         Return contentType
    23.     End Function
    24.  
    25.     Private Shared Function ScanFileForMimeType(ByVal fileName As String) As String
    26.         Dim buffer As Byte() = New Byte(255) {}
    27.         Using fs As New FileStream(fileName, FileMode.Open)
    28.             Dim readLength As Integer = CInt(Math.Min(256, fs.Length))
    29.             fs.Read(buffer, 0, readLength)
    30.         End Using
    31.         Try
    32.             Dim mimeType As UInt32
    33.             FindMimeFromData(0, Nothing, buffer, 256, Nothing, 0, mimeType, 0)
    34.             Dim mimeTypePtr As IntPtr = New IntPtr(mimeType)
    35.             Dim mime As String = Marshal.PtrToStringUni(mimeTypePtr)
    36.             Marshal.FreeCoTaskMem(mimeTypePtr)
    37.             If String.IsNullOrEmpty(mime) Then mime = "application/octet-stream"
    38.             Return mime
    39.         Catch e As Exception
    40.             Return "application/octet-stream"
    41.         End Try
    42.     End Function
    43.  
    44.     Private Shared Sub InitializeMimeTypeLists()
    45.         knownTypes = New List(Of String)
    46.         knownTypes = New String() {"text/plain", "text/html", "text/xml", "text/richtext", "text/scriptlet", _
    47.                                    "audio/x-aiff", "audio/basic", "audio/mid", "audio/wav", _
    48.                                    "image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/x-png", "image/tiff", "image/bmp", _
    49.                                    "image/x-xbitmap", "image/x-jg", "image/x-emf", "image/x-wmf", "video/avi", "video/mpeg", _
    50.                                    "application/octet-stream", "application/postscript", "application/base64", "application/macbinhex40", _
    51.                                    "application/pdf", "application/xml", "application/atom+xml", "application/rss+xml", _
    52.                                    "application/x-compressed", "application/x-zip-compressed", "application/x-gzip-compressed", _
    53.                                    "application/java", "application/x-msdownload" _
    54.                                    }.ToList
    55.  
    56.         mimeTypes = New Dictionary(Of String, String)
    57.         With mimeTypes
    58.             .Add("3dm", "x-world/x-3dmf")
    59.             .Add("3dmf", "x-world/x-3dmf")
    60.  
    61.             '...
    62.             ' get the complete list from mendhak's post here:
    63.             ' http://forums.asp.net/t/1143353.aspx
    64.             ' or from anywhere else if you can find a more recent updated list.
    65.             '...
    66.  
    67.             .Add("zoo", "application/octet-stream")
    68.             .Add("zsh", "text/x-script.zsh")
    69.         End With
    70.     End Sub
    71. End Class
    Last edited by Pradeep1210; Dec 3rd, 2010 at 06:30 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] MIME Type / ContentType problem

    Hey,

    Thanks for posting this.

    This will be very useful!!

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width