如何使用JavaScript从服务器读取文本文件?

在服务器上,有一个文本文件。我希望在客户端上使用JavaScript,我希望能够读取并处理该文件。服务器上文件的格式无法更改。


如何将文件内容转换为JavaScript变量,以便进行此处理?文件的大小最大为3.5 MB,但可以轻松地以100行(每行50-100个字符)的块进行处理。


用户不应该看到文件的任何内容;他将看到文件中数据处理的结果。


PIPIONE
浏览 2817回答 3
3回答

小怪兽爱吃肉

加载庞大的数据块并不是一个好的计划,但是如果需要的话,以下是如何使用jQuery$.ajax()函数来实现的概述。<html><head><script src="jquery.js"></script><script>getTxt = function (){&nbsp; $.ajax({&nbsp; &nbsp; url:'text.txt',&nbsp; &nbsp; success: function (data){&nbsp; &nbsp; &nbsp; //parse your data here&nbsp; &nbsp; &nbsp; //you can split into lines using data.split('\n')&nbsp;&nbsp; &nbsp; &nbsp; //an use regex functions to effectively parse it&nbsp; &nbsp; }&nbsp; });}</script></head><body>&nbsp; <button type="button" id="btnGetTxt" onclick="getTxt()">Get Text</button></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript