简体   繁体   English

用javascript读取txt文件

[英]read txt file with javascript

I am using this code for reading files. 我正在使用此代码读取文件。 Everything is OK with Chrome and FF, but IE does not update the data from the file ... seems to be reading from cache??? Chrome和FF一切正常,但是IE不会从文件中更新数据……似乎是从缓存中读取的??? Any suggestions? 有什么建议么? Thanks 谢谢

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","YOUR_FILE.txt",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseText;

You could try adding a dynamic string argument to the end of the filename to prevent caching. 您可以尝试在文件名的末尾添加动态字符串参数以防止缓存。

Try this: 尝试这个:

var time = new Date().getTime();
xmlhttp.open("GET", "YOUR_FILE.txt?time=" + time, false);

Check out Javascript's Date.getTime() documentation. 查看Javascript的Date.getTime()文档。

Please see http://msdn.microsoft.com/en-us/library/ms537505(v=vs.85).aspx 请参阅http://msdn.microsoft.com/en-us/library/ms537505(v=vs.85).aspx
Microsoft suggests slightly different code: 微软建议略有不同的代码:

var xmlHttp = null;
if (window.XMLHttpRequest) {
  // If IE7, Mozilla, Safari, and so on: Use native object.
  xmlHttp = new XMLHttpRequest();
}
else
{
  if (window.ActiveXObject) {
     // ...otherwise, use the ActiveX control for IE5.x and IE6.
     xmlHttp = new ActiveXObject('MSXML2.XMLHTTP.3.0');
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM