简体   繁体   English

用javascript打开本地文件系统中的文件

[英]Opening a file in local file system in javascript

I am looking out for a way to open a .xls file which is in temp directory using javascript in IE and Firefox browser.我正在寻找一种在 IE 和 Firefox 浏览器中使用 javascript 打开临时目录中的 .xls 文件的方法。 I tried using the javascript as follows,我尝试使用 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. names.xls 文件存在于那里,我已经验证过了。 As IE 7.0 does not let a user open a blank window due to security issues I am unable to make this work.由于安全问题,IE 7.0 不允许用户打开空白窗口,因此我无法完成这项工作。 I have not checked it with firefox yet.我还没有用firefox检查过它。 Is there any way to get this working?有没有办法让这个工作?

I also tried having an empty.html which has this javascript and calling this openMe() body onLoad.我也试过有一个 empty.html,它有这个 javascript 并在 onLoad 上调用这个 openMe() 主体。 And opening the empty.html from the parent HTML file.并从父 HTML 文件中打开 empty.html。 All I see is a new blank window without nothing but the file does not open.我所看到的只是一个新的空白窗口,什么也没有,但文件没有打开。

Any pointers would be greatly helpful.Thanks任何指针都会非常有帮助。谢谢

Cheers, Abi干杯,阿比

Sorry, Abi, you're out of luck--you can't use JavaScript in a browser to open a file on the local file system.抱歉,Abi,您走运了——您无法在浏览器中使用 JavaScript 打开本地文件系统上的文件。 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!您不希望人们在他们的网站上编写可以访问本地文件系统上的文件并可能从中读取数据的脚本!

HTML5 permits opening of local files as long as the computer user is selecting the files.只要计算机用户选择文件,HTML5 就允许打开本地文件。 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/您应该能够在此处找到有关 JavaScript API 的更多信息以及有关如何使用该 API 的示例代码: http : //www.html5rocks.com/en/tutorials/file/dndfiles/

Suggest you use AJAX.建议您使用 AJAX。 Small API follows.小 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 );
  }

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

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