is it possible to read a file using javascript?

后端 未结 6 1356
别那么骄傲
别那么骄傲 2020-12-07 04:25

Hi all i just want to know if we can read a file using javascript like

what we do

fp=(\"r\",\"path\")

like that is i

6条回答
  •  时光取名叫无心
    2020-12-07 04:58

    Firstly I think its not a good idea to read a file locally with JavaScript. I recommend first upload it to the server and then perform the reading.

    Having said that it is possible, but you restricted by what you can do.

    Im assuming its a local file on the user machine, otherwise AJAX would achieve this for a server read.

    It might be possible through

    1. Windows Script Host Object Model(WScript.Shell) and when granted Prompt or Enable access to ActiveX the browser has elevated privileges (Enable through Tools > Internet Options > Security > Custom Level ... > Set Active X settings to prompt). If this is still to difficult, user could download something thats installed and then does the reading through Shell Scripting! Disclaimer: Note I do not recommend this approach. Its not active for a reason and its so DIRTY (I feel dirty)!

    2. Cookies might also be worth considering. If you can store the information in a cookie the JavaScript would be able to read, write and update it.

      Found this code from http://www.quirksmode.org/js/cookies.html scroll right to the bottom for the example.

      function createCookie(name,value,days) {
      if (days) {
          var date = new Date();
          date.setTime(date.getTime()+(days*24*60*60*1000));
          var expires = "; expires="+date.toGMTString();
      }
      else var expires = "";
      document.cookie = name+"="+value+expires+"; path=/";
      

      }

      function readCookie(name) {
          var nameEQ = name + "=";
          var ca = document.cookie.split(';');
          for(var i=0;i < ca.length;i++) {
              var c = ca[i];
              while (c.charAt(0)==' ') c = c.substring(1,c.length);
              if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
          }
          return null;
      }
      
      function eraseCookie(name) {
          createCookie(name,"",-1);
      }
      

提交回复
热议问题