对于旧的*.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文章和这篇博客文章。
其他回答
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"
}
这篇文章将探讨在不同的编程语言中获取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>
如果有人想要Dirk Vollmar在c# switch语句中的答案:
case "doc": return "application/msword";
case "dot": return "application/msword";
case "docx": return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
case "dotx": return "application/vnd.openxmlformats-officedocument.wordprocessingml.template";
case "docm": return "application/vnd.ms-word.document.macroEnabled.12";
case "dotm": return "application/vnd.ms-word.template.macroEnabled.12";
case "xls": return "application/vnd.ms-excel";
case "xlt": return "application/vnd.ms-excel";
case "xla": return "application/vnd.ms-excel";
case "xlsx": return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
case "xltx": return "application/vnd.openxmlformats-officedocument.spreadsheetml.template";
case "xlsm": return "application/vnd.ms-excel.sheet.macroEnabled.12";
case "xltm": return "application/vnd.ms-excel.template.macroEnabled.12";
case "xlam": return "application/vnd.ms-excel.addin.macroEnabled.12";
case "xlsb": return "application/vnd.ms-excel.sheet.binary.macroEnabled.12";
case "ppt": return "application/vnd.ms-powerpoint";
case "pot": return "application/vnd.ms-powerpoint";
case "pps": return "application/vnd.ms-powerpoint";
case "ppa": return "application/vnd.ms-powerpoint";
case "pptx": return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
case "potx": return "application/vnd.openxmlformats-officedocument.presentationml.template";
case "ppsx": return "application/vnd.openxmlformats-officedocument.presentationml.slideshow";
case "ppam": return "application/vnd.ms-powerpoint.addin.macroEnabled.12";
case "pptm": return "application/vnd.ms-powerpoint.presentation.macroEnabled.12";
case "potm": return "application/vnd.ms-powerpoint.template.macroEnabled.12";
case "ppsm": return "application/vnd.ms-powerpoint.slideshow.macroEnabled.12";
case "mdb": return "application/vnd.ms-access";
加载一个。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")
}
}
以下是用于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文章和这篇博客文章。