admin管理员组

文章数量:1394593

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,

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?

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.

Any pointers would be greatly helpful.Thanks

Cheers, Abi

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,

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?

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.

Any pointers would be greatly helpful.Thanks

Cheers, Abi

Share Improve this question asked Apr 4, 2011 at 3:29 AbhishekAbhishek 6,91022 gold badges63 silver badges79 bronze badges 3
  • I believe you'll have less and less chance on getting this working as the newer browser versions are keeping tighter security.What is the primary scenario of this application? – sukru Commented Apr 4, 2011 at 3:33
  • possible duplicate of local file access with javascript – Andrew Marshall Commented Apr 4, 2011 at 3:35
  • Check out the latest HTML features: stackoverflow./a/7056216/356726 – Horst Walter Commented Jun 23, 2012 at 15:05
Add a ment  | 

3 Answers 3

Reset to default 3

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!

HTML5 permits opening of local files as long as the puter 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./en/tutorials/file/dndfiles/

Suggest you use AJAX. Small API follows.

/* **************************** AJAX ************************** */

///
/// Source
///   http://www.quirksmode/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 );
  }

本文标签: internet explorerOpening a file in local file system in javascriptStack Overflow