Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
What is the best way to provide mime type while downloading a file? I am using a download page which will write binary content and necessary headers to the response stream.
I have tried the following approaches.

1. Using Registry Key
C#
private string GetMimeType(FileInfo fileInfo)
{
    string mimeType = "application/unknown";
    RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(fileInfo.Extension.ToLower());
    if(regKey != null)
    {
        object contentType = regKey.GetValue("Content Type");
        if(contentType != null)
            mimeType = contentType.ToString();
    }
    return mimeType;
}

2. Using urlmon.dll
C#
[DllImport(@"urlmon.dll", CharSet = CharSet.Auto)]
private extern static System.UInt32 FindMimeFromData(
    System.UInt32 pBC,
    [MarshalAs(UnmanagedType.LPStr)] System.String pwzUrl,
    [MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer,
    System.UInt32 cbSize,
    [MarshalAs(UnmanagedType.LPStr)] System.String pwzMimeProposed,
    System.UInt32 dwMimeFlags,
    out System.UInt32 ppwzMimeOut,
    System.UInt32 dwReserverd
    );

These two methods were not useful as its not recognizing a lot of extensions from production server. Do you have any other suggestions/thoughts?
Posted
Updated 10-Feb-10 22:00pm
v2

1 solution

I would just store the mime types that I support, with a lookup of file extensions, on the server. Or just store the mime type and add it to the file table.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900