我正在使用JavaScript解析器创建一种新的语言,目前我正试图找出如何获取本地文件的内容。我尝试使用XMLHttp
请求,如下所示:
var rawFile = new XMLHttpRequest();
rawFile.open("GET", "testing.txt", true);
但那没用。我的终端出现了这个错误:
^{pr2}$我知道Python可以使用subprocess
模块获取文件的内容:
import subprocess
with open(file, 'r') as myfile:
data=myfile.read().replace('\n', '')`
那么我是否应该改用Python呢?或者有没有办法使用JavaScript?在
你是说像这样?在
function readFile(evt) { var file = (evt.target.files)[0]; var r = new FileReader(); r.onload = (function(file) { return function(e) { var contents = e.target.result; alert(contents); }; })(file); r.readAsText(file); } document.getElementById('fileinput').addEventListener('change', readFile, false);
; ^{pr2}$ ; 第13章; 第13章;相关问题 更多 >
编程相关推荐