none
如何根据文件路径 获取MIME类型,在线等!!! RRS feed

  • 问题

  • 我想获取这个文件的mime类型,然后用程序输出这个类型的文件,请老大们帮忙,我在网上找到了一些方法都是 根据获取的扩展名匹配 做好的MIME类型表,有没有直接获取的方法?
    2010年1月18日 1:06

答案

  • using System;
    using System.Runtime.InteropServices;
    using System.IO;
    
    namespace tools
    {
        public class t
        {
            [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
            );
    
            public string getMimeFromFile(string filename)
            {
                if (!File.Exists(filename))
                    throw new FileNotFoundException(filename + " not found");
    
                byte[] buffer = new byte[256];
                using (FileStream fs = new FileStream(filename, FileMode.Open))
                {
                    if (fs.Length >= 256)
                        fs.Read(buffer, 0, 256);
                    else
                        fs.Read(buffer, 0, (int)fs.Length);
                }
                try
                {
                    System.UInt32 mimetype;
                    FindMimeFromData(0, null, buffer, 256, null, 0, out mimetype, 0);
                    System.IntPtr mimeTypePtr = new IntPtr(mimetype);
                    string mime = Marshal.PtrToStringUni(mimeTypePtr);
                    Marshal.FreeCoTaskMem(mimeTypePtr);
                    return mime;
                }
                catch (Exception e)
                {
                    return "unknown/unknown";
                }
            }
        }
    }
    refer to http://stackoverflow.com/questions/58510/in-c-how-can-you-find-the-mime-type-of-a-file-based-on-the-file-signature-not-th
    2010年1月18日 5:42
  • 如 likecode所示 读取文件开头的256个字节就可以判断的
    参考http://stackoverflow.com/questions/58510/in-c-how-can-you-find-the-mime-type-of-a-file-based-on-the-file-signature-not-th
    Memory all alone in the moonlight~My Blog in .NET~~~
    2010年1月18日 8:33
    版主

全部回复

  • using System;
    using System.Runtime.InteropServices;
    using System.IO;
    
    namespace tools
    {
        public class t
        {
            [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
            );
    
            public string getMimeFromFile(string filename)
            {
                if (!File.Exists(filename))
                    throw new FileNotFoundException(filename + " not found");
    
                byte[] buffer = new byte[256];
                using (FileStream fs = new FileStream(filename, FileMode.Open))
                {
                    if (fs.Length >= 256)
                        fs.Read(buffer, 0, 256);
                    else
                        fs.Read(buffer, 0, (int)fs.Length);
                }
                try
                {
                    System.UInt32 mimetype;
                    FindMimeFromData(0, null, buffer, 256, null, 0, out mimetype, 0);
                    System.IntPtr mimeTypePtr = new IntPtr(mimetype);
                    string mime = Marshal.PtrToStringUni(mimeTypePtr);
                    Marshal.FreeCoTaskMem(mimeTypePtr);
                    return mime;
                }
                catch (Exception e)
                {
                    return "unknown/unknown";
                }
            }
        }
    }
    refer to http://stackoverflow.com/questions/58510/in-c-how-can-you-find-the-mime-type-of-a-file-based-on-the-file-signature-not-th
    2010年1月18日 5:42
  • 4 exp.
    tools.t myc = new tools.t();
    
            Response.Write(myc.getMimeFromFile(Server.MapPath("~/web.config")));
    2010年1月18日 5:43
  • 如 likecode所示 读取文件开头的256个字节就可以判断的
    参考http://stackoverflow.com/questions/58510/in-c-how-can-you-find-the-mime-type-of-a-file-based-on-the-file-signature-not-th
    Memory all alone in the moonlight~My Blog in .NET~~~
    2010年1月18日 8:33
    版主