Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Determine Mime Type for server side files
Message
From
03/11/2006 16:41:04
 
 
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01166970
Message ID:
01167048
Views:
9
>Hello Team,
>
>I have a restricted folder that i am allowing authorized downloading from through a response.binarywrite fairly straight forward stuff. What I would like to do is get the content type for the file server side before I try to do the write. So i can set my Response.ContentType = "xx" correct without relying on the file extension and a big switch statement.
>
>My humble thanks in advance,

Ok I have the solution for those of you who are interested. For your coding pleasure I have expanded it in a manner that requires no using statements. urlmon.dll is part of IE

Joe.
[System.Runtime.InteropServices.DllImport("urlmon.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode, ExactSpelling = true, SetLastError = false)] 
static extern int FindMimeFromData(IntPtr pBC, 
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string pwzUrl,
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPArray, ArraySubType = System.Runtime.InteropServices.UnmanagedType.I1, SizeParamIndex = 3)] 
 byte[] pBuffer, int cbSize,
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string pwzMimeProposed, int dwMimeFlags, out IntPtr ppwzMimeOut, int dwReserved);    
    
    /// <summary>
    /// Ensures that file exists and retrieves the content type 
    /// </summary>
    /// <param name="strfileName"></param>
    /// <returns>Returns the Content MimeType </returns>
    public static string getMimeFromFile(string strFileName)
    {
        IntPtr mimeout;
        if (!System.IO.File.Exists(strFileName))
            throw new System.IO.FileNotFoundException(strFileName + " not found");

        int MaxContent = (int)new System.IO.FileInfo(strFileName).Length;
        if (MaxContent > 4096) MaxContent = 4096;
        System.IO.FileStream fs = System.IO.File.OpenRead(strFileName);


        byte[] buf = new byte[MaxContent];
        fs.Read(buf, 0, MaxContent);
        fs.Close();
        int result = FindMimeFromData(IntPtr.Zero, strFileName, buf, MaxContent, null, 0, out mimeout, 0);

        if (result != 0)
            throw System.Runtime.InteropServices.Marshal.GetExceptionForHR(result);

        string mime = System.Runtime.InteropServices.Marshal.PtrToStringUni(mimeout);
        System.Runtime.InteropServices.Marshal.FreeCoTaskMem(mimeout);
        return mime;
    }
    //rename crystal.jpg to .gif to test functionality!
    //string getImg = Environment.GetEnvironmentVariable("windir") + "\\Web\\WallPaper\\Crystal.gif";
    //string mime = getMimeFromFile(getImg);
~Joe Johnston USA

"If ye love wealth better than liberty, the tranquility of servitude better than the animated contest of freedom, go home from us in peace. We ask not your counsel or arms. Crouch down and lick the hands which feed you. May your chains set lightly upon you, and may posterity forget that ye were our countrymen."
~Samuel Adams

Previous
Reply
Map
View

Click here to load this message in the networking platform