|
-
Dec 1st, 2010, 05:48 AM
#1
[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?
-
Dec 1st, 2010, 07:41 AM
#2
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
-
Dec 2nd, 2010, 03:28 AM
#3
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.
-
Dec 2nd, 2010, 04:17 AM
#4
Re: MIME Type / ContentType problem
Hey,
On that, I am really not sure. I don't know what the combinations are. Sorry 
Gary
-
Dec 3rd, 2010, 06:16 AM
#5
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:
Option Strict On Imports System.Runtime.InteropServices Imports System.IO Public Class MimeTypes Private Shared knownTypes As List(Of String) Private Shared mimeTypes As Dictionary(Of String, String) <DllImport("urlmon.dll", CharSet:=CharSet.Auto)> _ 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 End Function Public Shared Function GetContentType(ByVal fileName As String) As String If knownTypes Is Nothing OrElse mimeTypes Is Nothing Then InitializeMimeTypeLists() Dim contentType As String = "" Dim extension As String = IO.Path.GetExtension(fileName).Replace(".", "").ToLower mimeTypes.TryGetValue(extension, contentType) If String.IsNullOrEmpty(contentType) OrElse knownTypes.Contains(contentType) Then Dim headerType As String = ScanFileForMimeType(fileName) If headerType <> "application/octet-stream" OrElse String.IsNullOrEmpty(contentType) Then contentType = headerType End If Return contentType End Function Private Shared Function ScanFileForMimeType(ByVal fileName As String) As String Dim buffer As Byte() = New Byte(255) {} Using fs As New FileStream(fileName, FileMode.Open) Dim readLength As Integer = CInt(Math.Min(256, fs.Length)) fs.Read(buffer, 0, readLength) End Using Try Dim mimeType As UInt32 FindMimeFromData(0, Nothing, buffer, 256, Nothing, 0, mimeType, 0) Dim mimeTypePtr As IntPtr = New IntPtr(mimeType) Dim mime As String = Marshal.PtrToStringUni(mimeTypePtr) Marshal.FreeCoTaskMem(mimeTypePtr) If String.IsNullOrEmpty(mime) Then mime = "application/octet-stream" Return mime Catch e As Exception Return "application/octet-stream" End Try End Function Private Shared Sub InitializeMimeTypeLists() knownTypes = New List(Of String) knownTypes = New String() {"text/plain", "text/html", "text/xml", "text/richtext", "text/scriptlet", _ "audio/x-aiff", "audio/basic", "audio/mid", "audio/wav", _ "image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/x-png", "image/tiff", "image/bmp", _ "image/x-xbitmap", "image/x-jg", "image/x-emf", "image/x-wmf", "video/avi", "video/mpeg", _ "application/octet-stream", "application/postscript", "application/base64", "application/macbinhex40", _ "application/pdf", "application/xml", "application/atom+xml", "application/rss+xml", _ "application/x-compressed", "application/x-zip-compressed", "application/x-gzip-compressed", _ "application/java", "application/x-msdownload" _ }.ToList mimeTypes = New Dictionary(Of String, String) With mimeTypes .Add("3dm", "x-world/x-3dmf") .Add("3dmf", "x-world/x-3dmf") '... ' get the complete list from mendhak's post here: ' http://forums.asp.net/t/1143353.aspx ' or from anywhere else if you can find a more recent updated list. '... .Add("zoo", "application/octet-stream") .Add("zsh", "text/x-script.zsh") End With End Sub End Class
Last edited by Pradeep1210; Dec 3rd, 2010 at 06:30 AM.
-
Dec 4th, 2010, 09:16 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|