當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
使用JS读取本地文件
生活随笔
收集整理的這篇文章主要介紹了
使用JS读取本地文件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
使用XMLHttpRequest發(fā)起請求,讀取本地文件,主要總結(jié)了三種方法,如下所示:
// 創(chuàng)建XMLHttpRequest請求 const xhrFile = new XMLHttpRequest(); // 建立連接,第三個參數(shù)為同步或異步請求 xhrFile.open("GET", filePath, false); // method 1 xhrFile.onload = function() {const allText = xhrFile.response;callback(allText) }// method 2xhrFile.onreadystatechange = function(){if(xhrFile.readyState == 4){console.log(xhrFile.responseText);} }// method 3 // xhrFile.overrideMimeType("text/plain"); xhrFile.addEventListener("load",function(c){response= c.target.response;console.log(response); }) xhrFile.send();總結(jié)
以上是生活随笔為你收集整理的使用JS读取本地文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何在CAD中输入带圈序号?
- 下一篇: 邮件服务器在企业网中的应用