简体   繁体   English

如何使用Firefox从JavaScript读取本地文件

[英]How can I read local files from JavaScript with Firefox

I have some standalone JavaScript code that I've written to use with MaxMSP (a visual language for MIDI and DSP) that I need to debug. 我已经编写了一些独立的JavaScript代码,这些代码需要与MaxMSP(用于MIDI和DSP的可视语言)一起使用进行调试。

I downloaded WebStorm to try it out. 我下载了WebStorm进行尝试。 When I try to execute code that reads a local file, I get the following error: 当我尝试执行读取本地文件的代码时,出现以下错误:

Exception... "Security error" code: "1000" nsresult: "0x805303e8 (NS_ERROR_DOM_SECURITY_ERR)" 异常...“安全错误”代码:“ 1000” nsresult:“ 0x805303e8(NS_ERROR_DOM_SECURITY_ERR)”

I know people have seen this before and I have tried all the suggestions I found to bypass security and enable local file reading from Firefox but I'm still not able to get this to work. 我知道人们以前已经看到过这种情况,并且我尝试了所有发现的建议,这些建议可以绕过安全性并允许从Firefox读取本地文件,但是我仍然无法使它起作用。

I'm wondering if something has changed in recent versions of Firefox that make all the old suggestions useless. 我想知道在Firefox的最新版本中是否发生了更改,从而使所有旧建议无用。

Would appreciate any suggestions. 将不胜感激任何建议。

Thanks, David 谢谢大卫

OK --- I figured this out! 好的---我知道了! The big problem for me is lack of knowledge of javascript libraries. 对我来说,最大的问题是缺乏对JavaScript库的了解。

The XMLHttpRequest works but it needs an extra line XMLHttpRequest有效,但需要额外的一行

xmlhttp.send(); xmlhttp.send();

I didn't know about that. 我不知道 So code that actually works to read data from a local file (assuming one has followed the instructions posted in other places to turn off security is as simple as 因此,实际上可以从本地文件读取数据的代码(假设其中一个遵循其他地方发布的说明来关闭安全性,则只需

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", filename, false); // Synchronous
xmlhttp.send();
var text = xmlhttp.responseText;

I hope this saves others some time --- it was a real PITA for me to figure this out. 我希望这可以节省一些时间---对我来说,这真是一个PITA。

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

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