javascript 用javascript打开本地文件系统中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5534297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening a file in local file system in javascript
提问by Abhishek
I am looking out for a way to open a .xls file which is in temp directory using javascript in IE and Firefox browser. I tried using the javascript as follows,
我正在寻找一种在 IE 和 Firefox 浏览器中使用 javascript 打开临时目录中的 .xls 文件的方法。我尝试使用 javascript 如下,
function openMe(){
var newwindow=window.open("file:///{path to temp dir}/names.xls","window2","");
}
The names.xls file exists there, I have verified it. As IE 7.0 does not let a user open a blank window due to security issues I am unable to make this work. I have not checked it with firefox yet. Is there any way to get this working?
names.xls 文件存在于那里,我已经验证过了。由于安全问题,IE 7.0 不允许用户打开空白窗口,因此我无法完成这项工作。我还没有用firefox检查过它。有没有办法让这个工作?
I also tried having an empty.html which has this javascript and calling this openMe() body onLoad. And opening the empty.html from the parent HTML file. All I see is a new blank window without nothing but the file does not open.
我也试过有一个 empty.html,它有这个 javascript 并在 onLoad 上调用这个 openMe() 主体。并从父 HTML 文件中打开 empty.html。我所看到的只是一个新的空白窗口,什么也没有,但文件没有打开。
Any pointers would be greatly helpful.Thanks
任何指针都会非常有帮助。谢谢
Cheers, Abi
干杯,阿比
回答by KP Taylor
Sorry, Abi, you're out of luck--you can't use JavaScript in a browser to open a file on the local file system. This is a security issue and makes perfect sense if you think about it; you wouldn't want people writing scripts on their web sites that can access files on your local file system and possibly read data from them!
抱歉,Abi,您走运了——您无法在浏览器中使用 JavaScript 打开本地文件系统上的文件。这是一个安全问题,如果您仔细考虑一下,这是完全有道理的;您不希望人们在他们的网站上编写可以访问本地文件系统上的文件并可能从中读取数据的脚本!
回答by Irfan
HTML5 permits opening of local files as long as the computer user is selecting the files. You should be able to find more information on the JavaScript API along with sample code on how to use that API here: http://www.html5rocks.com/en/tutorials/file/dndfiles/
只要计算机用户选择文件,HTML5 就允许打开本地文件。您应该能够在此处找到有关 JavaScript API 的更多信息以及有关如何使用该 API 的示例代码:http: //www.html5rocks.com/en/tutorials/file/dndfiles/
回答by Gus
Suggest you use AJAX. Small API follows.
建议您使用 AJAX。小 API 紧随其后。
/* **************************** AJAX ************************** */
///
/// Source
/// http://www.quirksmode.org/js/xmlhttp.html
/// XMLHttpRequestForms is a local auxiliary variable
var XMLHttpRequestForms =
[
function ( ) { return new XMLHttpRequest ( ); },
function ( ) { return new ActiveXObject ( "Msxml2.XMLHTTP" ); },
function ( ) { return new ActiveXObject ( "Msxml3.XMLHTTP" ); },
function ( ) { return new ActiveXObject ( "Microsoft.XMLHTTP" ); }
];
// ******************************************* createXMLHTTPObject
// local entry point
/// createXMLHTTPObject is a helper function
function createXMLHTTPObject ( )
{
var xmlhttp = false;
for ( var i = 0; ( i < XMLHttpRequestForms.length ); i++ )
{
try
{
xmlhttp = XMLHttpRequestForms [ i ] ( );
break;
}
catch ( e )
{
continue;
}
}
return ( xmlhttp );
}
/// ************************************************ read_contents
// global entry point
/// <synopsis>
/// read_contents ( url )
///
/// <summary>
/// retrieves the contents of the specified URL
///
/// <param name="url">
/// a string containing the URL whose contents are to be read
///
/// <returns>
/// a string containing the contents of the URL
///
/// <example>
/// var text = read_contents ( "footer.ini" );
function read_contents ( url )
{
var request = createXMLHTTPObject ( );
if ( !request )
{
return ( null );
}
request.open ( 'GET', url, false );
request.setRequestHeader ( 'Content-Type', 'text/html' );
request.send ( );
return ( request.responseText );
}