admin 管理员组文章数量: 1087135
在点击如下链接的时候,浏览器会直接打开文件,而不是下载文件。
<a href="abc.txt">点击下载</a>
问题就来了,当文件中包含中文等非ACCII编码字符,浏览器中预览就会乱码。
解决方案如下:
我们需要配置 nginx 或者 apache 服务器,明确 txt 文件的 content-type 和 charset
(1)nginx 配置
server {
listen 80;
server_name example;
location / {
root /path/to/your/files;
charset utf-8; # Ensure charset is specified as UTF-8
autoindex on; # Optional: enable directory listing
}
# Serve .txt files with the correct Content-Type
location ~ \.txt$ {
default_type text/plain;
charset utf-8; # Ensure charset is specified as UTF-8
}
}
(2)apache 配置
<VirtualHost *:80>
ServerName example
DocumentRoot /path/to/your/files
# Ensure the default charset is set to UTF-8
AddDefaultCharset UTF-8
# Configure specific file types with UTF-8 charset
<FilesMatch "\.(txt)$">
ForceType 'text/plain; charset=UTF-8'
</FilesMatch>
</VirtualHost>
我的开源项目
- course-tencent-cloud(酷瓜云课堂 - gitee仓库)
- course-tencent-cloud(酷瓜云课堂 - github仓库)
版权声明:本文标题:浏览器打开 txt 文件乱码终极解决方案,亲测有效! 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/b/1738868003a2007078.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论