对于旧的*.doc文档,这就足够了:
header("Content-Type: application/msword");
我应该为新的。docx文档使用什么MIME类型?另外,对于pptx和xlsx文档?
对于旧的*.doc文档,这就足够了:
header("Content-Type: application/msword");
我应该为新的。docx文档使用什么MIME类型?另外,对于pptx和xlsx文档?
当前回答
加载一个。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文章和这篇博客文章。
或者,如果你在。net v4.5或更高版本中工作,尝试使用System.Web.MimeMapping.GetMimeMapping(yourFileName)来获取MIME类型。这比硬编码字符串要好得多。
更新:在.Net Core中,同样可以使用FileExtensionContentTypeProvider.TryGetContentType()实现。
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"));
}
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"
}
WPS表格:
application/wps-office.xlsx