1

So I am trying to read a file in a .js script on a .cshtml button-click in visual studio. What I would like to know is if it is possible to just link a file read straight to a known file path without having to use the FileReader's input method? I have a .txt file that holds part of a link so that it's not hardcoded in the program and I can go in and change it if I ever need to.

I expect the code to, on the button click, go to the .js function (which it does), but then read the set .txt file's contents and store it in the filePath variable in the following command:

window.open('http://' + filePath + '/', '_blank');

is this possible?

Bbylsma
  • 25
  • 6
  • why does `filePath` need to be read from a .txt file? If you want to just change the filePath from time to time this seems like a overkill to me – DigitalJedi Jun 07 '19 at 16:00
  • You might find something useful in the following link. What you want seems to be in the question's example code: var fileContents = fileForUpload.files.item(0).getAsBinary(); I don't know if this is want you want; if not, you can get in contact with people on that thread to get more details. https://stackoverflow.com/questions/750032/reading-file-contents-on-the-client-side-in-javascript-in-various-browsers – AntiqTech Jun 07 '19 at 16:01
  • Thank you AntiqTech! @DigitalJedi because the link may be referenced in other places in the code, so I don't want to have to go back and find all the instances. I definitely could do that, but I would like to go this route if possible – Bbylsma Jun 07 '19 at 16:15
  • you could just use a global variable for that? – DigitalJedi Jun 07 '19 at 16:19
  • True, I could... – Bbylsma Jun 07 '19 at 19:00

1 Answers1

0

You can use the XMLHttpRequest to load a text file. In the example below the DIV named dummy will receive the loaded text. But instead you could also place the http.responseText into a var for further processing.

    <div id="test">dummy</div>
    <input id="reset" type="button" value="reset" onMouseUp="resetDIV()" />
    <input id="load" type="button" value="load unsafe" onMouseUp="httpRequest('http://yourserver/yourpath/hello.txt')" />
    <input id="load" type="button" value="load safer" onMouseUp="httpRequestByID(1)" />
    const test = document.getElementById('test');
    const http = new XMLHttpRequest();

    function resetDIV() {
       test.innerHTML = 'dummy';
    }

    const httpResult = function() {
      console.log(http);
      test.innerHTML = http.responseText;
    }

    function httpRequest(_url) {
      // hacker unsafe because if a hacker finds a way to add or modify an element in the DOM and call your function with his own injected URL he can look for files you don't want others to see.
      http.open('GET', _url);
      http.onloadend = httpResult;
      http.send();
    }

    function httpRequestByID(_fileNum) {
      // a little safer function it is when the urls are hard typed and you can only reference by a file number
      if((typeof _fileNum)=='number' && _fileNum>0 && _fileNum<3){
         if(_fileNum===1){http.open('GET', 'http://yourserver/yourpath/hello.txt');}
         if(_fileNum===2){http.open('GET', 'http://yourserver/yourpath/other.txt');}
         http.onloadend = httpResult;
         http.send();
      }else{
         alert('httpRequestByID received an invalid argument');
         // nothing more should happen
      }
    }
2x2p
  • 414
  • 3
  • 17
  • footnote: never allow any user input as url to be sent as request, this would be an open door for hackers to scan for files on your server !!! – 2x2p Jun 07 '19 at 22:41
  • one of many ways to make it strict and therefor safer is to use `const` for the related elements/objects and reference a url by a `number` only. This way you are sure your function will never load a file you didn't allow to be loaded. – 2x2p Jun 10 '19 at 10:44