為了便于可視化,下面是以下記錄查找表。
我似乎無法在網上找到任何地方告訴你其中哪些也應該包含charset=utf-8.
我應該假設它與文本類似嗎?
看一看:
const MEDIA_TYPES: Record<string, string> = {
".md": "text/markdown",
".html": "text/html",
".htm": "text/html",
".json": "application/json",
".map": "application/json",
".txt": "text/plain",
".ts": "text/typescript",
".tsx": "text/tsx",
".js": "application/javascript",
".jsx": "text/jsx",
".gz": "application/gzip",
".css": "text/css",
".wasm": "application/wasm",
".mjs": "application/javascript",
".otf": "font/otf",
".ttf": "font/ttf",
".woff": "font/woff",
".woff2": "font/woff2",
".conf": "text/plain",
".list": "text/plain",
".log": "text/plain",
".ini": "text/plain",
".vtt": "text/vtt",
".yaml": "text/yaml",
".yml": "text/yaml",
".mid": "audio/midi",
".midi": "audio/midi",
".mp3": "audio/mp3",
".mp4a": "audio/mp4",
".m4a": "audio/mp4",
".ogg": "audio/ogg",
".spx": "audio/ogg",
".opus": "audio/ogg",
".wav": "audio/wav",
".webm": "audio/webm",
".aac": "audio/x-aac",
".flac": "audio/x-flac",
".mp4": "video/mp4",
".mp4v": "video/mp4",
".mkv": "video/x-matroska",
".mov": "video/quicktime",
".svg": "image/svg xml",
".avif": "image/avif",
".bmp": "image/bmp",
".gif": "image/gif",
".heic": "image/heic",
".heif": "image/heif",
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".png": "image/png",
".tiff": "image/tiff",
".psd": "image/vnd.adobe.photoshop",
".ico": "image/vnd.microsoft.icon",
".webp": "image/webp",
".es": "application/ecmascript",
".epub": "application/epub zip",
".jar": "application/java-archive",
".war": "application/java-archive",
".webmanifest": "application/manifest json",
".doc": "application/msword",
".dot": "application/msword",
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
".dotx": "application/vnd.openxmlformats-officedocument.wordprocessingml.template",
".cjs": "application/node",
".bin": "application/octet-stream",
".pkg": "application/octet-stream",
".dump": "application/octet-stream",
".exe": "application/octet-stream",
".deploy": "application/octet-stream",
".img": "application/octet-stream",
".msi": "application/octet-stream",
".pdf": "application/pdf",
".pgp": "application/pgp-encrypted",
".asc": "application/pgp-signature",
".sig": "application/pgp-signature",
".ai": "application/postscript",
".eps": "application/postscript",
".ps": "application/postscript",
".rdf": "application/rdf xml",
".rss": "application/rss xml",
".rtf": "application/rtf",
".apk": "application/vnd.android.package-archive",
".key": "application/vnd.apple.keynote",
".numbers": "application/vnd.apple.keynote",
".pages": "application/vnd.apple.pages",
".geo": "application/vnd.dynageo",
".gdoc": "application/vnd.google-apps.document",
".gslides": "application/vnd.google-apps.presentation",
".gsheet": "application/vnd.google-apps.spreadsheet",
".kml": "application/vnd.google-earth.kml xml",
".mkz": "application/vnd.google-earth.kmz",
".icc": "application/vnd.iccprofile",
".icm": "application/vnd.iccprofile",
".xls": "application/vnd.ms-excel",
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
".xlm": "application/vnd.ms-excel",
".ppt": "application/vnd.ms-powerpoint",
".pot": "application/vnd.ms-powerpoint",
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
".potx": "application/vnd.openxmlformats-officedocument.presentationml.template",
".xps": "application/vnd.ms-xpsdocument",
".odc": "application/vnd.oasis.opendocument.chart",
".odb": "application/vnd.oasis.opendocument.database",
".odf": "application/vnd.oasis.opendocument.formula",
".odg": "application/vnd.oasis.opendocument.graphics",
".odp": "application/vnd.oasis.opendocument.presentation",
".ods": "application/vnd.oasis.opendocument.spreadsheet",
".odt": "application/vnd.oasis.opendocument.text",
".rar": "application/vnd.rar",
".unityweb": "application/vnd.unity",
".dmg": "application/x-apple-diskimage",
".bz": "application/x-bzip",
".crx": "application/x-chrome-extension",
".deb": "application/x-debian-package",
".php": "application/x-httpd-php",
".iso": "application/x-iso9660-image",
".sh": "application/x-sh",
".sql": "application/x-sql",
".srt": "application/x-subrip",
".xml": "application/xml",
".zip": "application/zip",
};
/** Returns the content-type based on the extension of a path. */
function contentType(pathname: string): string | undefined {
return MEDIA_TYPES[pathname];
}
console.log(contentType("/dashboard/v1/index.html"));
console.log(contentType("/dashboard/v1/logo.svg"));
uj5u.com熱心網友回復:
MDN 說:
例如,對于主要型別為文本的任何 MIME 型別,您可以添加可選的 charset 引數來指定用于資料中字符的字符集。如果未指定字符集,則默認為 ASCII (US-ASCII),除非被用戶代理的設定覆寫。要指定 UTF-8 文本檔案,請使用 MIME 型別 text/plain;charset=UTF-8。
因此,對于任何基于text/...您的內容,您都可以選擇添加字符集。
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#structure_of_a_mime_type
以下contentType()功能更新演示了一種解決方案。
/** Returns the content-type based on the extension of a path. */
function contentType(ext: string): string | undefined {
const charsetUtf8 = "; charset=UTF-8";
// Content types that do not start with text/.. but usually contain charset=utf-8
const specialCase = [
"application/json",
"application/xml",
"application/rss xml",
];
let outputContentType = MEDIA_TYPES[ext];
// Return undefined.
if(!outputContentType) return;
if(outputContentType.startsWith("text/") || specialCase.includes(outputContentType)) {
// Combine Content-Type with charset=utf-8
outputContentType = outputContentType charsetUtf8;
// Return combined.
return outputContentType;
} else {
// Return for any other types or undefined.
return outputContentType;
}
}
然后從理論上講,您可以422: Unprocessable Entity在 mime 型別串列中不存在的任何內容型別上回傳 a 。
let requestedFilePath = path.join("./", context.main.http.server.root, urlPathNameParsed.dir, urlPathNameParsed.base);
try {
// try as file...
const file = await Deno.readFile(requestedFilePath);
// If file extension is not on media list...
if(typeof(contentType(urlPathNameParsed.ext)) === 'undefined') {
// Set Content-Type header for this response
responseHeaders.set('Content-Type', 'text/plain; charset=UTF-8');
await e.respondWith(
new Response("HTTP 422: Unprocessable Entity", {
headers: responseHeaders,
status: 422,
statusText: 'Unprocessable Entity',
})
);
} else {
// Set Content-Type header for this response
responseHeaders.set('Content-Type', `${contentType(urlPathNameParsed.ext)}`);
// Build and send the response
await e.respondWith(
new Response(file, {
headers: responseHeaders,
status: 200,
statusText: 'OK'
})
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/407278.html
標籤:
