鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ window.open()打開txt文件中文亂碼

window.open()打開txt文件中文亂碼

在js代碼里用以下代碼打開txt文件會造成中文亂碼。請問有什么解決方法嗎?

let newWin = window.open('_blank');
newWin.location = 'a.txt';
//newWin.document.charset="UTF-8";
console.log('88888:',newWin.document.charset)
newWin.document.characterSet="UTF-8";

使用了charset等幾種屬性賦值,但是charset等屬性都是只讀屬性,所以這種方法沒有效果。
圖片描述

回答
編輯回答
心上人

var newUrl= "data:text/html;charset=utf-8,"+ encodeURI(url).replace(/#/g,"%23");
window.open(newUrl);

2018年9月11日 15:10
編輯回答
淺時光

在后端設(shè)置header('Content-Type: text/plain; charset=utf-8')。
或者不要在新頁面打開,直接在當(dāng)前網(wǎng)頁里用<pre>或者<code>標(biāo)簽插入txt文件的內(nèi)容。

2018年4月1日 09:25