Opening a file in local file system in javascript

后端 未结 3 728
攒了一身酷
攒了一身酷 2020-11-30 16:00

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,



        
相关标签:
3条回答
  • 2020-11-30 16:16

    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/

    0 讨论(0)
  • 2020-11-30 16:20

    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!

    0 讨论(0)
  • 2020-11-30 16:26

    Suggest you use AJAX. Small API follows.

    /* **************************** 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 );
      }
    
    0 讨论(0)
提交回复
热议问题