如下代碼實現javascript讀取本地文件打印到瀏覽器中javascript
function load(name) { let xhr = new XMLHttpRequest(), okStatus = document.location.protocol === "file:" ? 0 : 200; xhr.open('GET', name, false); xhr.overrideMimeType("text/html;charset=utf-8");//默認爲utf-8 xhr.send(null); return xhr.status === okStatus ? xhr.responseText : null; } let text = load("文件名.txt"); console.log(text); //輸出到瀏覽器控制器中 //document.write(text); //打印在網頁中