如何在运行时获取文件的内容/类型

发布于 2024-10-10 07:09:15 字数 765 浏览 1 评论 0原文

我正在尝试动态确定输入文件的内容/类型。如果我在 Windows 应用程序中,我可以编写这样的代码(来自此博客

private string GetContentType(string fileName) {
    string contentType = "application/octetstream";
    string ext = System.IO.Path.GetExtension(fileName).ToLower();
    Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
    if (registryKey != null && registryKey.GetValue("Content Type") != null)
        contentType = registryKey.GetValue("Content Type").ToString();
    return contentType;
} 

还有哪些其他方法更适合 MVC 应用程序?

我想在 Controller.File(...) 方法中使用接收文件路径和内容类型的参数。

I am trying to determine dynamically the content/type of a input file. If I would be in a windows application I could write code like this (from this blog)

private string GetContentType(string fileName) {
    string contentType = "application/octetstream";
    string ext = System.IO.Path.GetExtension(fileName).ToLower();
    Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
    if (registryKey != null && registryKey.GetValue("Content Type") != null)
        contentType = registryKey.GetValue("Content Type").ToString();
    return contentType;
} 

What other methods are more suitable for an MVC application?

I would like to use the param within the Controller.File(...) method that receive a filepath and a contentype.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

月寒剑心 2024-10-17 07:09:15

在.Net 4.5中,您可以使用 MimeMapping.GetMimeMapping

string contentType = MimeMapping.GetMimeMapping("someFileName.pdf")
// contentType = "application/pdf"

更多信息

In .Net 4.5 you can use MimeMapping.GetMimeMapping:

string contentType = MimeMapping.GetMimeMapping("someFileName.pdf")
// contentType = "application/pdf"

More information

盗琴音 2024-10-17 07:09:15

重要说明:此答案写于 2011 年,比 masterwok 的答案早几年被写了。虽然我的答案仍然可以接受,但 masterwok 的答案更好、更简洁。请改用该答案并对其进行投票。以下所有内容仅供后代使用。

我只会使用文件扩展名,而不是尝试做一些聪明的事情,最终可能会反咬你一口。 :)

文件扩展名不需要在您的系统上注册(尽管我不知道您正在使用该文件做什么...)。如果您想过滤掉文件,您可以使用诸如枚举或数据库表之类的内容,其中包含有关可接受的扩展名的信息。

请参阅@Tolgahan 的想法。我基于此创建了一个下面的 C# 枚举,如果人们希望为此创建一个基于 db/enum/xml 的方法,它应该为人们提供一个起点。

Public enum MimeTypes
{
  [Description("application/postscript")]
  ai,
  [Description("audio/x-aiff")]
  aif,
  [Description("audio/x-aiff")]
  aifc,
  [Description("audio/x-aiff")]
  aiff,
  [Description("text/plain")]
  asc,
  [Description("application/atom+xml")]
  atom,
  [Description("audio/basic")]
  au,
  [Description("video/x-msvideo")]
  avi,
  [Description("application/x-bcpio")]
  bcpio,
  [Description("application/octet-stream")]
  bin,
  [Description("image/bmp")]
  bmp,
  [Description("application/x-netcdf")]
  cdf,
  [Description("image/cgm")]
  cgm,
  [Description("application/octet-stream")]
  class,
  [Description("application/x-cpio")]
  cpio,
  [Description("application/mac-compactpro")]
  cpt,
  [Description("application/x-csh")]
  csh,
  [Description("text/css")]
  css,
  [Description("application/x-director")]
  dcr,
  [Description("video/x-dv")]
  dif,
  [Description("application/x-director")]
  dir,
  [Description("image/vnd.djvu")]
  djv,
  [Description("image/vnd.djvu")]
  djvu,
  [Description("application/octet-stream")]
  dll,
  [Description("application/octet-stream")]
  dmg,
  [Description("application/octet-stream")]
  dms,
  [Description("application/msword")]
  doc,
  [Description("application/xml-dtd")]
  dtd,
  [Description("video/x-dv")]
  dv,
  [Description("application/x-dvi")]
  dvi,
  [Description("application/x-director")]
  dxr,
  [Description("application/postscript")]
  eps,
  [Description("text/x-setext")]
  etx,
  [Description("application/octet-stream")]
  exe,
  [Description("application/andrew-inset")]
  ez,
  [Description("image/gif")]
  gif,
  [Description("application/srgs")]
  gram,
  [Description("application/srgs+xml")]
  grxml,
  [Description("application/x-gtar")]
  gtar,
  [Description("application/x-hdf")]
  hdf,
  [Description("application/mac-binhex40")]
  hqx,
  [Description("text/html")]
  htm,
  [Description("text/html")]
  html,
  [Description("x-conference/x-cooltalk")]
  ice,
  [Description("image/x-icon")]
  ico,
  [Description("text/calendar")]
  ics,
  [Description("image/ief")]
  ief,
  [Description("text/calendar")]
  ifb,
  [Description("model/iges")]
  iges,
  [Description("model/iges")]
  igs,
  [Description("application/x-java-jnlp-file")]
  jnlp,
  [Description("image/jp2")]
  jp2,
  [Description("image/jpeg")]
  jpe,
  [Description("image/jpeg")]
  jpeg,
  [Description("image/jpeg")]
  jpg,
  [Description("application/x-javascript")]
  js,
  [Description("audio/midi")]
  kar,
  [Description("application/x-latex")]
  latex,
  [Description("application/octet-stream")]
  lha,
  [Description("application/octet-stream")]
  lzh,
  [Description("audio/x-mpegurl")]
  m3u,
  [Description("audio/mp4a-latm")]
  m4a,
  [Description("audio/mp4a-latm")]
  m4b,
  [Description("audio/mp4a-latm")]
  m4p,
  [Description("video/vnd.mpegurl")]
  m4u,
  [Description("video/x-m4v")]
  m4v,
  [Description("image/x-macpaint")]
  mac,
  [Description("application/x-troff-man")]
  man,
  [Description("application/mathml+xml")]
  mathml,
  [Description("application/x-troff-me")]
  me,
  [Description("model/mesh")]
  mesh,
  [Description("audio/midi")]
  mid,
  [Description("audio/midi")]
  midi,
  [Description("application/vnd.mif")]
  mif,
  [Description("video/quicktime")]
  mov,
  [Description("video/x-sgi-movie")]
  movie,
  [Description("audio/mpeg")]
  mp2,
  [Description("audio/mpeg")]
  mp3,
  [Description("video/mp4")]
  mp4,
  [Description("video/mpeg")]
  mpe,
  [Description("video/mpeg")]
  mpeg,
  [Description("video/mpeg")]
  mpg,
  [Description("audio/mpeg")]
  mpga,
  [Description("application/x-troff-ms")]
  ms,
  [Description("model/mesh")]
  msh,
  [Description("video/vnd.mpegurl")]
  mxu,
  [Description("application/x-netcdf")]
  nc,
  [Description("application/oda")]
  oda,
  [Description("application/ogg")]
  ogg,
  [Description("image/x-portable-bitmap")]
  pbm,
  [Description("image/pict")]
  pct,
  [Description("chemical/x-pdb")]
  pdb,
  [Description("application/pdf")]
  pdf,
  [Description("image/x-portable-graymap")]
  pgm,
  [Description("application/x-chess-pgn")]
  pgn,
  [Description("image/pict")]
  pic,
  [Description("image/pict")]
  pict,
  [Description("image/png")]
  png,
  [Description("image/x-portable-anymap")]
  pnm,
  [Description("image/x-macpaint")]
  pnt,
  [Description("image/x-macpaint")]
  pntg,
  [Description("image/x-portable-pixmap")]
  ppm,
  [Description("application/vnd.ms-powerpoint")]
  ppt,
  [Description("application/postscript")]
  ps,
  [Description("video/quicktime")]
  qt,
  [Description("image/x-quicktime")]
  qti,
  [Description("image/x-quicktime")]
  qtif,
  [Description("audio/x-pn-realaudio")]
  ra,
  [Description("audio/x-pn-realaudio")]
  ram,
  [Description("image/x-cmu-raster")]
  ras,
  [Description("application/rdf+xml")]
  rdf,
  [Description("image/x-rgb")]
  rgb,
  [Description("application/vnd.rn-realmedia")]
  rm,
  [Description("application/x-troff")]
  roff,
  [Description("text/rtf")]
  rtf,
  [Description("text/richtext")]
  rtx,
  [Description("text/sgml")]
  sgm,
  [Description("text/sgml")]
  sgml,
  [Description("application/x-sh")]
  sh,
  [Description("application/x-shar")]
  shar,
  [Description("model/mesh")]
  silo,
  [Description("application/x-stuffit")]
  sit,
  [Description("application/x-koan")]
  skd,
  [Description("application/x-koan")]
  skm,
  [Description("application/x-koan")]
  skp,
  [Description("application/x-koan")]
  skt,
  [Description("application/smil")]
  smi,
  [Description("application/smil")]
  smil,
  [Description("audio/basic")]
  snd,
  [Description("application/octet-stream")]
  so,
  [Description("application/x-futuresplash")]
  spl,
  [Description("application/x-wais-source")]
  src,
  [Description("application/x-sv4cpio")]
  sv4cpio,
  [Description("application/x-sv4crc")]
  sv4crc,
  [Description("image/svg+xml")]
  svg,
  [Description("application/x-shockwave-flash")]
  swf,
  [Description("application/x-troff")]
  t,
  [Description("application/x-tar")]
  tar,
  [Description("application/x-tcl")]
  tcl,
  [Description("application/x-tex")]
  tex,
  [Description("application/x-texinfo")]
  texi,
  [Description("application/x-texinfo")]
  texinfo,
  [Description("image/tiff")]
  tif,
  [Description("image/tiff")]
  tiff,
  [Description("application/x-troff")]
  tr,
  [Description("text/tab-separated-values")]
  tsv,
  [Description("text/plain")]
  txt,
  [Description("application/x-ustar")]
  ustar,
  [Description("application/x-cdlink")]
  vcd,
  [Description("model/vrml")]
  vrml,
  [Description("application/voicexml+xml")]
  vxml,
  [Description("audio/x-wav")]
  wav,
  [Description("image/vnd.wap.wbmp")]
  wbmp,
  [Description("application/vnd.wap.wbxml")]
  wbmxl,
  [Description("text/vnd.wap.wml")]
  wml,
  [Description("application/vnd.wap.wmlc")]
  wmlc,
  [Description("text/vnd.wap.wmlscript")]
  wmls,
  [Description("application/vnd.wap.wmlscriptc")]
  wmlsc,
  [Description("model/vrml")]
  wrl,
  [Description("image/x-xbitmap")]
  xbm,
  [Description("application/xhtml+xml")]
  xht,
  [Description("application/xhtml+xml")]
  xhtml,
  [Description("application/vnd.ms-excel")]
  xls,
  [Description("application/xml")]
  xml,
  [Description("image/x-xpixmap")]
  xpm,
  [Description("application/xml")]
  xsl,
  [Description("application/xslt+xml")]
  xslt,
  [Description("application/vnd.mozilla.xul+xml")]
  xul,
  [Description("image/x-xwindowdump")]
  xwd,
  [Description("chemical/x-xyz")]
  xyz,
  [Description("application/zip")]
  zip
}

Important note: This answer was written in 2011, a few years before masterwok's answer was written. Whilst my answer is still acceptable, masterwok's answer is better and more concise. Use and upvote that answer instead. Everything below is for posterity.

I would just use the file extension rather than try to do something clever that may eventually come back to bite you in the arse. :)

The file extension doesn't need to be registered on your system (although I don't know exactly what you're doing with the file...). You could use something like an enum or db table which contains information on acceptable extensions if you want to filter out files.

Please see @Tolgahan's idea on this. I created a C# enum below based on this which should provide people with a starting point should they wish to create a db/enum/xml-based approach to this.

Public enum MimeTypes
{
  [Description("application/postscript")]
  ai,
  [Description("audio/x-aiff")]
  aif,
  [Description("audio/x-aiff")]
  aifc,
  [Description("audio/x-aiff")]
  aiff,
  [Description("text/plain")]
  asc,
  [Description("application/atom+xml")]
  atom,
  [Description("audio/basic")]
  au,
  [Description("video/x-msvideo")]
  avi,
  [Description("application/x-bcpio")]
  bcpio,
  [Description("application/octet-stream")]
  bin,
  [Description("image/bmp")]
  bmp,
  [Description("application/x-netcdf")]
  cdf,
  [Description("image/cgm")]
  cgm,
  [Description("application/octet-stream")]
  class,
  [Description("application/x-cpio")]
  cpio,
  [Description("application/mac-compactpro")]
  cpt,
  [Description("application/x-csh")]
  csh,
  [Description("text/css")]
  css,
  [Description("application/x-director")]
  dcr,
  [Description("video/x-dv")]
  dif,
  [Description("application/x-director")]
  dir,
  [Description("image/vnd.djvu")]
  djv,
  [Description("image/vnd.djvu")]
  djvu,
  [Description("application/octet-stream")]
  dll,
  [Description("application/octet-stream")]
  dmg,
  [Description("application/octet-stream")]
  dms,
  [Description("application/msword")]
  doc,
  [Description("application/xml-dtd")]
  dtd,
  [Description("video/x-dv")]
  dv,
  [Description("application/x-dvi")]
  dvi,
  [Description("application/x-director")]
  dxr,
  [Description("application/postscript")]
  eps,
  [Description("text/x-setext")]
  etx,
  [Description("application/octet-stream")]
  exe,
  [Description("application/andrew-inset")]
  ez,
  [Description("image/gif")]
  gif,
  [Description("application/srgs")]
  gram,
  [Description("application/srgs+xml")]
  grxml,
  [Description("application/x-gtar")]
  gtar,
  [Description("application/x-hdf")]
  hdf,
  [Description("application/mac-binhex40")]
  hqx,
  [Description("text/html")]
  htm,
  [Description("text/html")]
  html,
  [Description("x-conference/x-cooltalk")]
  ice,
  [Description("image/x-icon")]
  ico,
  [Description("text/calendar")]
  ics,
  [Description("image/ief")]
  ief,
  [Description("text/calendar")]
  ifb,
  [Description("model/iges")]
  iges,
  [Description("model/iges")]
  igs,
  [Description("application/x-java-jnlp-file")]
  jnlp,
  [Description("image/jp2")]
  jp2,
  [Description("image/jpeg")]
  jpe,
  [Description("image/jpeg")]
  jpeg,
  [Description("image/jpeg")]
  jpg,
  [Description("application/x-javascript")]
  js,
  [Description("audio/midi")]
  kar,
  [Description("application/x-latex")]
  latex,
  [Description("application/octet-stream")]
  lha,
  [Description("application/octet-stream")]
  lzh,
  [Description("audio/x-mpegurl")]
  m3u,
  [Description("audio/mp4a-latm")]
  m4a,
  [Description("audio/mp4a-latm")]
  m4b,
  [Description("audio/mp4a-latm")]
  m4p,
  [Description("video/vnd.mpegurl")]
  m4u,
  [Description("video/x-m4v")]
  m4v,
  [Description("image/x-macpaint")]
  mac,
  [Description("application/x-troff-man")]
  man,
  [Description("application/mathml+xml")]
  mathml,
  [Description("application/x-troff-me")]
  me,
  [Description("model/mesh")]
  mesh,
  [Description("audio/midi")]
  mid,
  [Description("audio/midi")]
  midi,
  [Description("application/vnd.mif")]
  mif,
  [Description("video/quicktime")]
  mov,
  [Description("video/x-sgi-movie")]
  movie,
  [Description("audio/mpeg")]
  mp2,
  [Description("audio/mpeg")]
  mp3,
  [Description("video/mp4")]
  mp4,
  [Description("video/mpeg")]
  mpe,
  [Description("video/mpeg")]
  mpeg,
  [Description("video/mpeg")]
  mpg,
  [Description("audio/mpeg")]
  mpga,
  [Description("application/x-troff-ms")]
  ms,
  [Description("model/mesh")]
  msh,
  [Description("video/vnd.mpegurl")]
  mxu,
  [Description("application/x-netcdf")]
  nc,
  [Description("application/oda")]
  oda,
  [Description("application/ogg")]
  ogg,
  [Description("image/x-portable-bitmap")]
  pbm,
  [Description("image/pict")]
  pct,
  [Description("chemical/x-pdb")]
  pdb,
  [Description("application/pdf")]
  pdf,
  [Description("image/x-portable-graymap")]
  pgm,
  [Description("application/x-chess-pgn")]
  pgn,
  [Description("image/pict")]
  pic,
  [Description("image/pict")]
  pict,
  [Description("image/png")]
  png,
  [Description("image/x-portable-anymap")]
  pnm,
  [Description("image/x-macpaint")]
  pnt,
  [Description("image/x-macpaint")]
  pntg,
  [Description("image/x-portable-pixmap")]
  ppm,
  [Description("application/vnd.ms-powerpoint")]
  ppt,
  [Description("application/postscript")]
  ps,
  [Description("video/quicktime")]
  qt,
  [Description("image/x-quicktime")]
  qti,
  [Description("image/x-quicktime")]
  qtif,
  [Description("audio/x-pn-realaudio")]
  ra,
  [Description("audio/x-pn-realaudio")]
  ram,
  [Description("image/x-cmu-raster")]
  ras,
  [Description("application/rdf+xml")]
  rdf,
  [Description("image/x-rgb")]
  rgb,
  [Description("application/vnd.rn-realmedia")]
  rm,
  [Description("application/x-troff")]
  roff,
  [Description("text/rtf")]
  rtf,
  [Description("text/richtext")]
  rtx,
  [Description("text/sgml")]
  sgm,
  [Description("text/sgml")]
  sgml,
  [Description("application/x-sh")]
  sh,
  [Description("application/x-shar")]
  shar,
  [Description("model/mesh")]
  silo,
  [Description("application/x-stuffit")]
  sit,
  [Description("application/x-koan")]
  skd,
  [Description("application/x-koan")]
  skm,
  [Description("application/x-koan")]
  skp,
  [Description("application/x-koan")]
  skt,
  [Description("application/smil")]
  smi,
  [Description("application/smil")]
  smil,
  [Description("audio/basic")]
  snd,
  [Description("application/octet-stream")]
  so,
  [Description("application/x-futuresplash")]
  spl,
  [Description("application/x-wais-source")]
  src,
  [Description("application/x-sv4cpio")]
  sv4cpio,
  [Description("application/x-sv4crc")]
  sv4crc,
  [Description("image/svg+xml")]
  svg,
  [Description("application/x-shockwave-flash")]
  swf,
  [Description("application/x-troff")]
  t,
  [Description("application/x-tar")]
  tar,
  [Description("application/x-tcl")]
  tcl,
  [Description("application/x-tex")]
  tex,
  [Description("application/x-texinfo")]
  texi,
  [Description("application/x-texinfo")]
  texinfo,
  [Description("image/tiff")]
  tif,
  [Description("image/tiff")]
  tiff,
  [Description("application/x-troff")]
  tr,
  [Description("text/tab-separated-values")]
  tsv,
  [Description("text/plain")]
  txt,
  [Description("application/x-ustar")]
  ustar,
  [Description("application/x-cdlink")]
  vcd,
  [Description("model/vrml")]
  vrml,
  [Description("application/voicexml+xml")]
  vxml,
  [Description("audio/x-wav")]
  wav,
  [Description("image/vnd.wap.wbmp")]
  wbmp,
  [Description("application/vnd.wap.wbxml")]
  wbmxl,
  [Description("text/vnd.wap.wml")]
  wml,
  [Description("application/vnd.wap.wmlc")]
  wmlc,
  [Description("text/vnd.wap.wmlscript")]
  wmls,
  [Description("application/vnd.wap.wmlscriptc")]
  wmlsc,
  [Description("model/vrml")]
  wrl,
  [Description("image/x-xbitmap")]
  xbm,
  [Description("application/xhtml+xml")]
  xht,
  [Description("application/xhtml+xml")]
  xhtml,
  [Description("application/vnd.ms-excel")]
  xls,
  [Description("application/xml")]
  xml,
  [Description("image/x-xpixmap")]
  xpm,
  [Description("application/xml")]
  xsl,
  [Description("application/xslt+xml")]
  xslt,
  [Description("application/vnd.mozilla.xul+xml")]
  xul,
  [Description("image/x-xwindowdump")]
  xwd,
  [Description("chemical/x-xyz")]
  xyz,
  [Description("application/zip")]
  zip
}
铁憨憨 2024-10-17 07:09:15

.NET CORE
使用 Microsoft.AspNetCore.StaticFiles

var provider = new FileExtensionContentTypeProvider();
string contentType;
if(!provider.TryGetContentType(fileName, out contentType))
{
    contentType = "application/octet-stream";
}

.NET CORE
Using Microsoft.AspNetCore.StaticFiles

var provider = new FileExtensionContentTypeProvider();
string contentType;
if(!provider.TryGetContentType(fileName, out contentType))
{
    contentType = "application/octet-stream";
}
榆西 2024-10-17 07:09:15

IDEA:放置格式和扩展数据(http://www.feedforall.com/mime-types.htm) htm) 到 xml 或您的项目中作为字典、数组或其他内容进行查询,并创建一个过程来确定扩展的 mimetype。我认为将数据保存在 xml 文档中将更容易在编译后进行修改

IDEA: put the formats and extensions data (http://www.feedforall.com/mime-types.htm) into an xml or into your project as dictionary, array or sth else for query and create a procedure for determine mimetype for extension.. i think keeping your data on xml document will be easier for modification after compiling

狠疯拽 2024-10-17 07:09:15

如果你想使用流

    public class urlmonMimeDetect
    {
        [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 static string GetMimeFromFile(Stream fs)
        {

            byte[] buffer = new byte[256];
            fs.Read(buffer, 0, 256);

            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";
            }
        }
    }

If you want to use stream

    public class urlmonMimeDetect
    {
        [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 static string GetMimeFromFile(Stream fs)
        {

            byte[] buffer = new byte[256];
            fs.Read(buffer, 0, 256);

            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";
            }
        }
    }
墨洒年华 2024-10-17 07:09:15

文件是否正在上传到 MVC 应用程序?

public ActionResult FileUploader(HttpPostedFileBase upload)
{
  string mimeType=upload.ContentType;
}

不过,有一些警告。

它使用浏览器提供的又名“客户端提供的”mime 类型,如果上传项目的用户未经身份验证或信任,那么这可能不是一个好主意。

此外,IE 并不总是为某些文件提供标准 Mime 类型,特别是 PNG(请参阅 “image/png”和“image/x-png”之间有什么区别?)这可能会也可能不会影响您的应用程序。

我们只是使用一个简单的 if 语句将 IE png mime 类型 (image/x-png) 转换回更常见的类型 (image/png)。

Is the file being uploaded to the MVC application?

public ActionResult FileUploader(HttpPostedFileBase upload)
{
  string mimeType=upload.ContentType;
}

There are a couple of caveats with this though.

It uses the browser supplied aka 'client supplied' mime type, if the users uploading items aren't authenticated or trusted, then this might not be a good idea.

Also IE doesn't always give standard Mime types for certain files, in particular PNGs (see What is the difference between "image/png" and "image/x-png"?) which may or may not effect your application.

We just use a simple if statement to convert the IE png mime type (image/x-png), back to the more common one (image/png).

苦妄 2024-10-17 07:09:15

如果您使用的是类似 linux/unix 的系统,您可以使用 file 命令来确定文件类型。

共有三组测试,按此顺序执行:文件系统测试、幻数测试和语言测试。第一个成功的测试会导致打印文件类型。

如果您使用的是 Windows 计算机,通常会使用代码中所示的文件扩展名。

If you are on a linux/unix like system you can use the file command to determine the file type.

There are three sets of tests, performed in this order: filesystem tests, magic number tests, and language tests. The first test that succeeds causes the file type to be printed.

If you're on a Windows machine it is common to use the file extension like you showed in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文