除了使用 ActiveX 之外,还有什么方法可以在 javascript 中读取文本文件吗?
鉴于所有文件(html 文件、文本文件等)都在网络上, 除了使用 ActiveX 之外,还有什么方法可以读取文本文件并将其打印在文本区域中吗?
我已经尝试过这样的操作,但它没有达到目标:
function getSelectedItem(){ var client = new XMLHttpRequest(); if(document.codeForm.dropList.value == "foo") client.open('GET', 'foo.txt'); else if(document.codeForm.dropList.value == "bar") client.open('GET', 'bar.txt'); client.onreadystatechange = function() { //This actually displays the message in the file alert(client.responseText); //But this doesn't. This just displays "undefined" // document.codeForm.source.value = client.reponseText; } client.send(); }
由于我实际上可以使用文件上下文显示警报消息,因此我相信会有某种方法可以做到这一点。 (实际上文件的内容似乎进入“client.reponseText”, 但它的数据类型是 DOMstring,而不仅仅是 String。)
任何建议将不胜感激。 谢谢。
Given all the files (html file, text files, etc) are on the web,
is there any way to read text file and print them in a textarea beside using ActiveX?
I've tried like this, but it didn't reach the goal:
function getSelectedItem(){ var client = new XMLHttpRequest(); if(document.codeForm.dropList.value == "foo") client.open('GET', 'foo.txt'); else if(document.codeForm.dropList.value == "bar") client.open('GET', 'bar.txt'); client.onreadystatechange = function() { //This actually displays the message in the file alert(client.responseText); //But this doesn't. This just displays "undefined" // document.codeForm.source.value = client.reponseText; } client.send(); }
Since I could actually display alert message with the file context, I believe there would be some way to do this.
(Actually the contents of files seem to come into "client.reponseText",
but it's data type is DOMstring, not just String.)
Any advice would be very appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 jQuery。 http://api.jquery.com/jQuery.get/
Use jQuery. http://api.jquery.com/jQuery.get/
尝试这个
,
或者
你的文本区域将需要一个 id 属性来使用最后两种方法
try this instead
or
or
your textarea will need an id attribute to use the last two methods