对于旧的*.doc文档,这就足够了:
header("Content-Type: application/msword");
我应该为新的。docx文档使用什么MIME类型?另外,对于pptx和xlsx文档?
对于旧的*.doc文档,这就足够了:
header("Content-Type: application/msword");
我应该为新的。docx文档使用什么MIME类型?另外,对于pptx和xlsx文档?
当前回答
以下是用于HTTP内容流的正确Microsoft Office MIME类型:
Extension MIME Type
.doc application/msword
.dot application/msword
.docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
.dotx application/vnd.openxmlformats-officedocument.wordprocessingml.template
.docm application/vnd.ms-word.document.macroEnabled.12
.dotm application/vnd.ms-word.template.macroEnabled.12
.xls application/vnd.ms-excel
.xlt application/vnd.ms-excel
.xla application/vnd.ms-excel
.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xltx application/vnd.openxmlformats-officedocument.spreadsheetml.template
.xlsm application/vnd.ms-excel.sheet.macroEnabled.12
.xltm application/vnd.ms-excel.template.macroEnabled.12
.xlam application/vnd.ms-excel.addin.macroEnabled.12
.xlsb application/vnd.ms-excel.sheet.binary.macroEnabled.12
.ppt application/vnd.ms-powerpoint
.pot application/vnd.ms-powerpoint
.pps application/vnd.ms-powerpoint
.ppa application/vnd.ms-powerpoint
.pptx application/vnd.openxmlformats-officedocument.presentationml.presentation
.potx application/vnd.openxmlformats-officedocument.presentationml.template
.ppsx application/vnd.openxmlformats-officedocument.presentationml.slideshow
.ppam application/vnd.ms-powerpoint.addin.macroEnabled.12
.pptm application/vnd.ms-powerpoint.presentation.macroEnabled.12
.potm application/vnd.ms-powerpoint.template.macroEnabled.12
.ppsm application/vnd.ms-powerpoint.slideshow.macroEnabled.12
.mdb application/vnd.ms-access
欲了解更多细节,请查看这篇TechNet文章和这篇博客文章。
其他回答
android中用于填充映射列表mime类型的工作方法。
private static void fileMimeTypeMapping() {
MIMETYPE_MAPPING.put("3gp", Collections.list("video/3gpp"));
MIMETYPE_MAPPING.put("7z", Collections.list("application/x-7z-compressed"));
MIMETYPE_MAPPING.put("accdb", Collections.list("application/msaccess"));
MIMETYPE_MAPPING.put("ai", Collections.list("application/illustrator"));
MIMETYPE_MAPPING.put("apk", Collections.list("application/vnd.android.package-archive"));
MIMETYPE_MAPPING.put("arw", Collections.list("image/x-dcraw"));
MIMETYPE_MAPPING.put("avi", Collections.list("video/x-msvideo"));
MIMETYPE_MAPPING.put("bash", Collections.list("text/x-shellscript"));
MIMETYPE_MAPPING.put("bat", Collections.list("application/x-msdos-program"));
MIMETYPE_MAPPING.put("blend", Collections.list("application/x-blender"));
MIMETYPE_MAPPING.put("bin", Collections.list("application/x-bin"));
MIMETYPE_MAPPING.put("bmp", Collections.list("image/bmp"));
MIMETYPE_MAPPING.put("bpg", Collections.list("image/bpg"));
MIMETYPE_MAPPING.put("bz2", Collections.list("application/x-bzip2"));
MIMETYPE_MAPPING.put("cb7", Collections.list("application/x-cbr"));
MIMETYPE_MAPPING.put("cba", Collections.list("application/x-cbr"));
MIMETYPE_MAPPING.put("cbr", Collections.list("application/x-cbr"));
MIMETYPE_MAPPING.put("cbt", Collections.list("application/x-cbr"));
MIMETYPE_MAPPING.put("cbtc", Collections.list("application/x-cbr"));
MIMETYPE_MAPPING.put("cbz", Collections.list("application/x-cbr"));
MIMETYPE_MAPPING.put("cc", Collections.list("text/x-c"));
MIMETYPE_MAPPING.put("cdr", Collections.list("application/coreldraw"));
MIMETYPE_MAPPING.put("class", Collections.list("application/java"));
MIMETYPE_MAPPING.put("cnf", Collections.list("text/plain"));
MIMETYPE_MAPPING.put("conf", Collections.list("text/plain"));
MIMETYPE_MAPPING.put("cpp", Collections.list("text/x-c++src"));
MIMETYPE_MAPPING.put("cr2", Collections.list("image/x-dcraw"));
MIMETYPE_MAPPING.put("css", Collections.list("text/css"));
MIMETYPE_MAPPING.put("csv", Collections.list("text/csv"));
MIMETYPE_MAPPING.put("cvbdl", Collections.list("application/x-cbr"));
MIMETYPE_MAPPING.put("c", Collections.list("text/x-c"));
MIMETYPE_MAPPING.put("c++", Collections.list("text/x-c++src"));
MIMETYPE_MAPPING.put("dcr", Collections.list("image/x-dcraw"));
MIMETYPE_MAPPING.put("deb", Collections.list("application/x-deb"));
MIMETYPE_MAPPING.put("dng", Collections.list("image/x-dcraw"));
MIMETYPE_MAPPING.put("doc", Collections.list("application/msword"));
MIMETYPE_MAPPING.put("docm", Collections.list("application/vnd.ms-word.document.macroEnabled.12"));
MIMETYPE_MAPPING.put("docx", Collections.list("application/vnd.openxmlformats-officedocument.wordprocessingml.document"));
MIMETYPE_MAPPING.put("dot", Collections.list("application/msword"));
MIMETYPE_MAPPING.put("dotx", Collections.list("application/vnd.openxmlformats-officedocument.wordprocessingml.template"));
MIMETYPE_MAPPING.put("dv", Collections.list("video/dv"));
MIMETYPE_MAPPING.put("eot", Collections.list("application/vnd.ms-fontobject"));
MIMETYPE_MAPPING.put("epub", Collections.list("application/epub+zip"));
MIMETYPE_MAPPING.put("eps", Collections.list("application/postscript"));
MIMETYPE_MAPPING.put("erf", Collections.list("image/x-dcraw"));
MIMETYPE_MAPPING.put("exe", Collections.list("application/x-ms-dos-executable"));
MIMETYPE_MAPPING.put("flac", Collections.list("audio/flac"));
MIMETYPE_MAPPING.put("flv", Collections.list("video/x-flv"));
MIMETYPE_MAPPING.put("gif", Collections.list("image/gif"));
MIMETYPE_MAPPING.put("gpx", Collections.list("application/gpx+xml"));
MIMETYPE_MAPPING.put("gz", Collections.list("application/gzip"));
MIMETYPE_MAPPING.put("gzip", Collections.list("application/gzip"));
MIMETYPE_MAPPING.put("h", Collections.list("text/x-h"));
MIMETYPE_MAPPING.put("heic", Collections.list("image/heic"));
MIMETYPE_MAPPING.put("heif", Collections.list("image/heif"));
MIMETYPE_MAPPING.put("hh", Collections.list("text/x-h"));
MIMETYPE_MAPPING.put("hpp", Collections.list("text/x-h"));
MIMETYPE_MAPPING.put("htaccess", Collections.list("text/plain"));
MIMETYPE_MAPPING.put("ical", Collections.list("text/calendar"));
MIMETYPE_MAPPING.put("ics", Collections.list("text/calendar"));
MIMETYPE_MAPPING.put("iiq", Collections.list("image/x-dcraw"));
MIMETYPE_MAPPING.put("impress", Collections.list("text/impress"));
MIMETYPE_MAPPING.put("java", Collections.list("text/x-java-source"));
MIMETYPE_MAPPING.put("jp2", Collections.list("image/jp2"));
MIMETYPE_MAPPING.put("jpeg", Collections.list("image/jpeg"));
MIMETYPE_MAPPING.put("jpg", Collections.list("image/jpeg"));
MIMETYPE_MAPPING.put("jps", Collections.list("image/jpeg"));
MIMETYPE_MAPPING.put("k25", Collections.list("image/x-dcraw"));
MIMETYPE_MAPPING.put("kdc", Collections.list("image/x-dcraw"));
MIMETYPE_MAPPING.put("key", Collections.list("application/x-iwork-keynote-sffkey"));
MIMETYPE_MAPPING.put("keynote", Collections.list("application/x-iwork-keynote-sffkey"));
MIMETYPE_MAPPING.put("kml", Collections.list("application/vnd.google-earth.kml+xml"));
MIMETYPE_MAPPING.put("kmz", Collections.list("application/vnd.google-earth.kmz"));
MIMETYPE_MAPPING.put("kra", Collections.list("application/x-krita"));
MIMETYPE_MAPPING.put("ldif", Collections.list("text/x-ldif"));
MIMETYPE_MAPPING.put("love", Collections.list("application/x-love-game"));
MIMETYPE_MAPPING.put("lwp", Collections.list("application/vnd.lotus-wordpro"));
MIMETYPE_MAPPING.put("m2t", Collections.list("video/mp2t"));
MIMETYPE_MAPPING.put("m3u", Collections.list("audio/mpegurl"));
MIMETYPE_MAPPING.put("m3u8", Collections.list("audio/mpegurl"));
MIMETYPE_MAPPING.put("m4a", Collections.list("audio/mp4"));
MIMETYPE_MAPPING.put("m4b", Collections.list("audio/m4b"));
MIMETYPE_MAPPING.put("m4v", Collections.list("video/mp4"));
MIMETYPE_MAPPING.put("markdown", Collections.list(MIMETYPE_TEXT_MARKDOWN));
MIMETYPE_MAPPING.put("mdown", Collections.list(MIMETYPE_TEXT_MARKDOWN));
MIMETYPE_MAPPING.put("md", Collections.list(MIMETYPE_TEXT_MARKDOWN));
MIMETYPE_MAPPING.put("mdb", Collections.list("application/msaccess"));
MIMETYPE_MAPPING.put("mdwn", Collections.list(MIMETYPE_TEXT_MARKDOWN));
MIMETYPE_MAPPING.put("mkd", Collections.list(MIMETYPE_TEXT_MARKDOWN));
MIMETYPE_MAPPING.put("mef", Collections.list("image/x-dcraw"));
MIMETYPE_MAPPING.put("mkv", Collections.list("video/x-matroska"));
MIMETYPE_MAPPING.put("mobi", Collections.list("application/x-mobipocket-ebook"));
MIMETYPE_MAPPING.put("mov", Collections.list("video/quicktime"));
MIMETYPE_MAPPING.put("mp3", Collections.list("audio/mpeg"));
MIMETYPE_MAPPING.put("mp4", Collections.list("video/mp4"));
MIMETYPE_MAPPING.put("mpeg", Collections.list("video/mpeg"));
MIMETYPE_MAPPING.put("mpg", Collections.list("video/mpeg"));
MIMETYPE_MAPPING.put("mpo", Collections.list("image/jpeg"));
MIMETYPE_MAPPING.put("msi", Collections.list("application/x-msi"));
MIMETYPE_MAPPING.put("mts", Collections.list("video/MP2T"));
MIMETYPE_MAPPING.put("mt2s", Collections.list("video/MP2T"));
MIMETYPE_MAPPING.put("nef", Collections.list("image/x-dcraw"));
MIMETYPE_MAPPING.put("numbers", Collections.list("application/x-iwork-numbers-sffnumbers"));
MIMETYPE_MAPPING.put("odf", Collections.list("application/vnd.oasis.opendocument.formula"));
MIMETYPE_MAPPING.put("odg", Collections.list("application/vnd.oasis.opendocument.graphics"));
MIMETYPE_MAPPING.put("odp", Collections.list("application/vnd.oasis.opendocument.presentation"));
MIMETYPE_MAPPING.put("ods", Collections.list("application/vnd.oasis.opendocument.spreadsheet"));
MIMETYPE_MAPPING.put("odt", Collections.list("application/vnd.oasis.opendocument.text"));
MIMETYPE_MAPPING.put("oga", Collections.list("audio/ogg"));
MIMETYPE_MAPPING.put("ogg", Collections.list("audio/ogg"));
MIMETYPE_MAPPING.put("ogv", Collections.list("video/ogg"));
MIMETYPE_MAPPING.put("one", Collections.list("application/msonenote"));
MIMETYPE_MAPPING.put("opus", Collections.list("audio/ogg"));
MIMETYPE_MAPPING.put("orf", Collections.list("image/x-dcraw"));
MIMETYPE_MAPPING.put("otf", Collections.list("application/font-sfnt"));
MIMETYPE_MAPPING.put("pages", Collections.list("application/x-iwork-pages-sffpages"));
MIMETYPE_MAPPING.put("pdf", Collections.list("application/pdf"));
MIMETYPE_MAPPING.put("pfb", Collections.list("application/x-font"));
MIMETYPE_MAPPING.put("pef", Collections.list("image/x-dcraw"));
MIMETYPE_MAPPING.put("php", Collections.list("application/x-php"));
MIMETYPE_MAPPING.put("pl", Collections.list("application/x-perl"));
MIMETYPE_MAPPING.put("pls", Collections.list("audio/x-scpls"));
MIMETYPE_MAPPING.put("png", Collections.list("image/png"));
MIMETYPE_MAPPING.put("pot", Collections.list("application/vnd.ms-powerpoint"));
MIMETYPE_MAPPING.put("potm", Collections.list("application/vnd.ms-powerpoint.template.macroEnabled.12"));
MIMETYPE_MAPPING.put("potx", Collections.list("application/vnd.openxmlformats-officedocument.presentationml.template"));
MIMETYPE_MAPPING.put("ppa", Collections.list("application/vnd.ms-powerpoint"));
MIMETYPE_MAPPING.put("ppam", Collections.list("application/vnd.ms-powerpoint.addin.macroEnabled.12"));
MIMETYPE_MAPPING.put("pps", Collections.list("application/vnd.ms-powerpoint"));
MIMETYPE_MAPPING.put("ppsm", Collections.list("application/vnd.ms-powerpoint.slideshow.macroEnabled.12"));
MIMETYPE_MAPPING.put("ppsx", Collections.list("application/vnd.openxmlformats-officedocument.presentationml.slideshow"));
MIMETYPE_MAPPING.put("ppt", Collections.list("application/vnd.ms-powerpoint"));
MIMETYPE_MAPPING.put("pptm", Collections.list("application/vnd.ms-powerpoint.presentation.macroEnabled.12"));
MIMETYPE_MAPPING.put("pptx", Collections.list("application/vnd.openxmlformats-officedocument.presentationml.presentation"));
MIMETYPE_MAPPING.put("ps", Collections.list("application/postscript"));
MIMETYPE_MAPPING.put("psd", Collections.list("application/x-photoshop"));
MIMETYPE_MAPPING.put("py", Collections.list("text/x-python"));
MIMETYPE_MAPPING.put("raf", Collections.list("image/x-dcraw"));
MIMETYPE_MAPPING.put("rar", Collections.list("application/x-rar-compressed"));
MIMETYPE_MAPPING.put("reveal", Collections.list("text/reveal"));
MIMETYPE_MAPPING.put("rss", Collections.list("application/rss+xml"));
MIMETYPE_MAPPING.put("rtf", Collections.list("application/rtf"));
MIMETYPE_MAPPING.put("rw2", Collections.list("image/x-dcraw"));
MIMETYPE_MAPPING.put("schema", Collections.list("text/plain"));
MIMETYPE_MAPPING.put("sgf", Collections.list("application/sgf"));
MIMETYPE_MAPPING.put("sh-lib", Collections.list("text/x-shellscript"));
MIMETYPE_MAPPING.put("sh", Collections.list("text/x-shellscript"));
MIMETYPE_MAPPING.put("srf", Collections.list("image/x-dcraw"));
MIMETYPE_MAPPING.put("sr2", Collections.list("image/x-dcraw"));
MIMETYPE_MAPPING.put("tar", Collections.list("application/x-tar"));
MIMETYPE_MAPPING.put("tar.bz2", Collections.list("application/x-bzip2"));
MIMETYPE_MAPPING.put("tar.gz", Collections.list("application/x-compressed"));
MIMETYPE_MAPPING.put("tbz2", Collections.list("application/x-bzip2"));
MIMETYPE_MAPPING.put("tcx", Collections.list("application/vnd.garmin.tcx+xml"));
MIMETYPE_MAPPING.put("tex", Collections.list("application/x-tex"));
MIMETYPE_MAPPING.put("tgz", Collections.list("application/x-compressed"));
MIMETYPE_MAPPING.put("tiff", Collections.list("image/tiff"));
MIMETYPE_MAPPING.put("tif", Collections.list("image/tiff"));
MIMETYPE_MAPPING.put("ttf", Collections.list("application/font-sfnt"));
MIMETYPE_MAPPING.put("txt", Collections.list("text/plain"));
MIMETYPE_MAPPING.put("vcard", Collections.list("text/vcard"));
MIMETYPE_MAPPING.put("vcf", Collections.list("text/vcard"));
MIMETYPE_MAPPING.put("vob", Collections.list("video/dvd"));
MIMETYPE_MAPPING.put("vsd", Collections.list("application/vnd.visio"));
MIMETYPE_MAPPING.put("vsdm", Collections.list("application/vnd.ms-visio.drawing.macroEnabled.12"));
MIMETYPE_MAPPING.put("vsdx", Collections.list("application/vnd.ms-visio.drawing"));
MIMETYPE_MAPPING.put("vssm", Collections.list("application/vnd.ms-visio.stencil.macroEnabled.12"));
MIMETYPE_MAPPING.put("vssx", Collections.list("application/vnd.ms-visio.stencil"));
MIMETYPE_MAPPING.put("vstm", Collections.list("application/vnd.ms-visio.template.macroEnabled.12"));
MIMETYPE_MAPPING.put("vstx", Collections.list("application/vnd.ms-visio.template"));
MIMETYPE_MAPPING.put("wav", Collections.list("audio/wav"));
MIMETYPE_MAPPING.put("webm", Collections.list("video/webm"));
MIMETYPE_MAPPING.put("woff", Collections.list("application/font-woff"));
MIMETYPE_MAPPING.put("wpd", Collections.list("application/vnd.wordperfect"));
MIMETYPE_MAPPING.put("wmv", Collections.list("video/x-ms-wmv"));
MIMETYPE_MAPPING.put("xcf", Collections.list("application/x-gimp"));
MIMETYPE_MAPPING.put("xla", Collections.list("application/vnd.ms-excel"));
MIMETYPE_MAPPING.put("xlam", Collections.list("application/vnd.ms-excel.addin.macroEnabled.12"));
MIMETYPE_MAPPING.put("xls", Collections.list("application/vnd.ms-excel"));
MIMETYPE_MAPPING.put("xlsb", Collections.list("application/vnd.ms-excel.sheet.binary.macroEnabled.12"));
MIMETYPE_MAPPING.put("xlsm", Collections.list("application/vnd.ms-excel.sheet.macroEnabled.12"));
MIMETYPE_MAPPING.put("xlsx", Collections.list("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
MIMETYPE_MAPPING.put("xlt", Collections.list("application/vnd.ms-excel"));
MIMETYPE_MAPPING.put("xltm", Collections.list("application/vnd.ms-excel.template.macroEnabled.12"));
MIMETYPE_MAPPING.put("xltx", Collections.list("application/vnd.openxmlformats-officedocument.spreadsheetml.template"));
MIMETYPE_MAPPING.put("xrf", Collections.list("image/x-dcraw"));
MIMETYPE_MAPPING.put("yaml", Arrays.asList("application/yaml", "text/plain"));
MIMETYPE_MAPPING.put("yml", Arrays.asList("application/yaml", "text/plain"));
MIMETYPE_MAPPING.put("zip", Collections.list("application/zip"));
MIMETYPE_MAPPING.put("url", Collections.list("application/internet-shortcut"));
MIMETYPE_MAPPING.put("webloc", Collections.list("application/internet-shortcut"));
MIMETYPE_MAPPING.put("js", Arrays.asList("application/javascript", "text/plain"));
MIMETYPE_MAPPING.put("json", Arrays.asList("application/json", "text/plain"));
MIMETYPE_MAPPING.put("fb2", Arrays.asList("application/x-fictionbook+xml", "text/plain"));
MIMETYPE_MAPPING.put("html", Arrays.asList("text/html", "text/plain"));
MIMETYPE_MAPPING.put("htm", Arrays.asList("text/html", "text/plain"));
MIMETYPE_MAPPING.put("m", Arrays.asList("text/x-matlab", "text/plain"));
MIMETYPE_MAPPING.put("svg", Arrays.asList("image/svg+xml", "text/plain"));
MIMETYPE_MAPPING.put("swf", Arrays.asList("application/x-shockwave-flash", "application/octet-stream"));
MIMETYPE_MAPPING.put("xml", Arrays.asList("application/xml", "text/plain"));
}
加载一个。docx文件。
if let htmlFile = Bundle.main.path(forResource: "fileName", ofType: "docx") {
let url = URL(fileURLWithPath: htmlFile)
do{
let data = try Data(contentsOf: url)
self.webView.load(data, mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", textEncodingName: "UTF-8", baseURL: url)
}catch{
print("errrr")
}
}
这篇文章将探讨在不同的编程语言中获取MIME类型的各种方法,并将它们的CONS作为一行描述作为标题。所以,根据自己的情况使用它们,选择最适合自己的。
如。当用户可能提供.xls, .xlsx或.xlsm中的任何一个,而你不想编写代码测试扩展并为每个扩展提供mime类型时,下面的代码特别有用。让系统来做这项工作。
Python 3
使用python-magic
>>> pip install python-magic
>>> import magic
>>> magic.from_file("Employee.pdf", mime=True)
'application/pdf'
使用内置的MimeTypes模块-将文件名映射到MimeTypes模块
>>> import mimetypes
>>> mimetypes.init()
>>> mimetypes.knownfiles
['/etc/mime.types', '/etc/httpd/mime.types', ... ]
>>> mimetypes.suffix_map['.tgz']
'.tar.gz'
>>> mimetypes.encodings_map['.gz']
'gzip'
>>> mimetypes.types_map['.tgz']
'application/x-tar-gz'
JAVA 7
来源:Baeldung关于Java中的文件MIME类型的博客
操作系统相关
@Test
public void get_JAVA7_mimetype() {
Path path = new File("Employee.xlsx").toPath();
String mimeType = Files.probeContentType(path);
assertEquals(mimeType, "application/vnd.ms-excel");
}
它将使用filettypedetector实现探测MIME类型,并调用每个实现的probeContentType来解析该类型。因此,如果文件为实现所知,则返回内容类型。但是,如果没有发生这种情况,则调用系统默认的文件类型检测程序。
使用输入流的前几个字符进行解析
@Test
public void getMIMEType_from_Extension(){
File file = new File("Employee.xlsx");
String mimeType = URLConnection.guessContentTypeFromName(file.getName());
assertEquals(mimeType, "application/vnd.ms-excel");
}
使用内置的MIME类型表
@Test
public void getMIMEType_UsingGetFileNameMap(){
File file = new File("Employee.xlsx");
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String mimeType = fileNameMap.getContentTypeFor(file.getName());
assertEquals(mimeType, "image/png");
}
它返回URLConnection的所有实例使用的MIME类型矩阵,然后用于解析输入文件类型。然而,当涉及到URLConnection时,这个MIME类型矩阵是非常有限的。
默认情况下,该类使用content-types。属性文件在JRE_HOME/lib。但是,我们可以通过使用content.types.user.table属性指定一个特定于用户的表来扩展它:
System.setProperty("content.types.user.table","<path-to-file>");
JavaScript
来源:FileReader API & Medium关于在JavaScript中使用魔法数字获取Mime类型的文章
解释使用FileReader API获取的魔数
当使用javaScript根据文件流获取MimeType时,最终结果如下所示。打开嵌入式jsFiddle来查看和理解这种方法。
额外的好处:大多数MIME类型都可以访问,你也可以在getMimetype函数中添加自定义MIME类型。此外,它完全支持MS Office文件Mime类型。
在这个例子中,计算文件mime类型的步骤如下:
The user selects a file. Take the first 4 bytes of the file using the slice method. Create a new FileReader instance Use the FileReader to read the 4 bytes you sliced out as an array buffer. Since the array buffer is just a generic way to represent a binary buffer we need to create a TypedArray, in this case an Uint8Array. With a TypedArray at our hands we can retrieve every byte and transform it to hexadecimal (by using toString(16)). We now have a way to get the magic numbers from a file by reading the first four bytes. The final step is to map it to a real mime type.
浏览器支持(总体高于95%,在所有现代浏览器中接近100%):
const uploads = [] const fileSelector = document.getElementById('file-selector') fileSelector.addEventListener('change', (event) => { console.time('FileOpen') const file = event.target.files[0] const filereader = new FileReader() filereader.onloadend = function(evt) { if (evt.target.readyState === FileReader.DONE) { const uint = new Uint8Array(evt.target.result) let bytes = [] uint.forEach((byte) => { bytes.push(byte.toString(16)) }) const hex = bytes.join('').toUpperCase() uploads.push({ filename: file.name, filetype: file.type ? file.type : 'Unknown/Extension missing', binaryFileType: getMimetype(hex), hex: hex }) render() } console.timeEnd('FileOpen') } const blob = file.slice(0, 4); filereader.readAsArrayBuffer(blob); }) const render = () => { const container = document.getElementById('files') const uploadedFiles = uploads.map((file) => { return `<div class=result><hr /> <span class=filename>Filename: <strong>${file.filename}</strong></span><br> <span class=fileObject>File Object (Mime Type):<strong> ${file.filetype}</strong></span><br> <span class=binaryObject>Binary (Mime Type):<strong> ${file.binaryFileType}</strong></span><br> <span class=HexCode>Hex Code (Magic Number):<strong> <em>${file.hex}</strong></span></em> </div>` }) container.innerHTML = uploadedFiles.join('') } const getMimetype = (signature) => { switch (signature) { case '89504E47': return 'image/png' case '47494638': return 'image/gif' case '25504446': return 'application/pdf' case 'FFD8FFDB': case 'FFD8FFE0': case 'FFD8FFE1': return 'image/jpeg' case '504B0304': return 'application/zip' case '504B34': return 'application/vnd.ms-excel.sheet.macroEnabled.12' default: return 'Unknown filetype' } } .result { font-family: Palatino, "Palatino Linotype", "Palatino LT STD", "Book Antiqua", Georgia, serif; line-height: 20px; font-size: 14px; margin: 10px 0; } .filename { color: #333; font-size: 16px; } .fileObject { color: #a53; } .binaryObject { color: #63f; } .HexCode { color: #262; } em { padding: 2px 4px; background-color: #efefef; font-style: normal; } input[type=file] { background-color: #4CAF50; border: none; color: white; padding: 8px 16px; text-decoration: none; margin: 4px 2px; cursor: pointer; } <body> <input type="file" id="file-selector"> <div id="files"></div>
WPS表格:
application/wps-office.xlsx
Swift4
func mimeTypeForPath(path: String) -> String {
let url = NSURL(fileURLWithPath: path)
let pathExtension = url.pathExtension
if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension! as NSString, nil)?.takeRetainedValue() {
if let mimetype = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() {
return mimetype as String
}
}
return "application/octet-stream"
}