diff --git a/wwwroot/api/ticket_attachment.dspy b/wwwroot/api/ticket_attachment.dspy index 4c0606a..33270b1 100644 --- a/wwwroot/api/ticket_attachment.dspy +++ b/wwwroot/api/ticket_attachment.dspy @@ -30,4 +30,21 @@ else: headers['Content-Disposition'] = ( 'inline; filename="%s"; filename*=UTF-8\'\'%s' % (fname, safe_name)) +# 文本类显式带 charset=utf-8:aiohttp FileResponse 按扩展名猜 Content-Type 不附 +# charset,text/plain 无 charset 时浏览器按 ISO-8859-1 解 UTF-8 → 中文乱码 +# (2026-09-11 用户报障)。未知扩展名的纯文本(log/conf 等)也归入 text/plain 内联展示。 +import mimetypes as _mt +_ctype = _mt.guess_type(fname)[0] or '' +if not _ctype: + _ext = fname.rsplit('.', 1)[-1].lower() if '.' in fname else '' + if _ext in ('txt', 'log', 'md', 'csv', 'conf', 'ini', 'yml', 'yaml', + 'py', 'js', 'sh', 'sql', 'json'): + _ctype = 'text/plain' +if _ctype.startswith('text/') or _ctype in ( + 'application/json', 'application/javascript', + 'application/xml', 'image/svg+xml'): + _ctype += '; charset=utf-8' +if _ctype: + headers['Content-Type'] = _ctype + return FileResponse(full_path, headers=headers)