如何通过javascript从tomcat服务器下载文件?

时间:2013-07-09 08:59:09

标签: javascript tomcat download

我在Tomcat文件夹中保存了一个文本文件, c:\ program files / apache software foundation / tomcat7.0 / test / test.txt

我只能在我自己的电脑上的浏览器中打开它。

如何下​​载并将其保存到我的本地磁盘,例如: f:\ test.txt

我可以只使用Javascript吗? 任何示例代码?

万分感谢!

1 个答案:

答案 0 :(得分:0)

所有网络服务器都支持ajax。您只能从本地网络服务器尝试ajax。要上传到服务器,您需要购买域名和网站空间。联系任何网站托管公司,他们会指导您,例如:http://godaddy.com/。下面是从服务器读取文本文件的ajax代码

 var txtFile = new XMLHttpRequest();
    txtFile.open("GET", "http://www.example.com/data/data.txt", true);
    txtFile.onreadystatechange = function() {
      if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.
        if (txtFile.status === 200) {  // Makes sure it's found the file.
          allText = txtFile.responseText; 
          lines = txtFile.responseText.split("\n"); // Will separate each line into an array
        }
      }
    }
    txtFile.send(null);