menu JavaScript Studio

Search JavaScript help

Facebook: https://www.facebook.com/ArrowatLLC Twitter: https://twitter.com/ArrowatLLC Instagram: https://www.instagram.com/ArrowatLLC Youtube: https://www.youtube.com/channel/UCNZV1HjDgVNooR6ePpwTM_Q linkedin: https://www.linkedin.com/showcase/jscriptstudio Arrowat: https://arrowat.com/arrowat

File reading using JavaScript

What about read files you create on the app you are coding? in the version 2.0 was introduced file creation and file reading while you can still use that feature, in the version 4.0 we introduced file reading by coding JavaScript: You can read files created on the current app you are coding or from your computer libraries, Documents, Pictures Library, Video Library, Music Library, Storage Devices, the jscriptstudio.js file library works on JavaScript Studio Pro only. The following table explains how to read files.

Current Coding App

You can read ".js, .css, .txt" files you have created on the current app you are coding. Use the following javascript code to read these files. jss.File.Read("FILENAME", "FUNCTIONNAME");
Note: If you use this option to create a .js files, it requires you to write the file directlty, pasting code from other location or from clipboard the code may fail because the extra lines from code are interpreted as blank line in JavaScript Studio, if you want to paste JavaScript code from other location or from your clipboard and use it on JavaScript Studio file reading we suggest you to use the following HTML markup
<!--<ReadFile><Name>FileName </Name></ReadFile>-->
<ShowFileContentHere/>

Please read Using files as data information in JavaScript Studio tutorial to learn how use it.

Documents

You can read ".txt" from Documents folder. Use the following javascript code to read these files.
The following code reads the file from documents folder
jss.File.Read("FILENAME", "FUNCTIONNAME","Documents\\","\\");

The following code reads the filename from documents sub folder files
jss.File.Read("FILENAME", "FUNCTIONNAME","Documents\\files","\\");

The following code reads the filename from documents sub folder files\textfiles\
jss.File.Read("FILENAME", "FUNCTIONNAME","Documents\\files","\\textfiles\\");

Note that to read files from documents the JavaScript code requires 4 parameters the FUNCTIONNAME is where the file content is returned. the other parameters builds a path like Documents\files\filename.txt, Documents\filename.txt or Documents\files\textfiles\filename.txt and so on.

Pictures Library

You can read ".txt" from PicturesLibrary folder. Use the following javascript code to read these files.
The following code reads the file from PicturesLibrary folder
jss.File.Read("FILENAME", "FUNCTIONNAME","PicturesLibrary\\","\\");

The following code reads the filename from PicturesLibrary sub folder files
jss.File.Read("FILENAME", "FUNCTIONNAME","PicturesLibrary\\files","\\");

The following code reads the filename from PicturesLibrary sub folder files\textfiles\
jss.File.Read("FILENAME", "FUNCTIONNAME","PicturesLibrary\\files","\\textfiles\\");

Note that to read files from PicturesLibrary the JavaScript code requires 4 parameters the FUNCTIONNAME is where the file content is returned. the other parameters builds a path like PicturesLibrary\files\filename.txt, PicturesLibrary\filename.txt or PicturesLibrary\files\textfiles\filename.txt and so on.

Videos Library

You can read ".txt" from VideosLibrary folder. Use the following javascript code to read these files.
The following code reads the file from VideosLibrary folder
jss.File.Read("FILENAME", "FUNCTIONNAME","VideosLibrary\\","\\");

The following code reads the filename from VideosLibrary sub folder files
jss.File.Read("FILENAME", "FUNCTIONNAME","VideosLibrary\\files","\\");

The following code reads the filename from VideosLibrary sub folder files\textfiles\
jss.File.Read("FILENAME", "FUNCTIONNAME","VideosLibrary\\files","\\textfiles\\");

Note that to read files from VideosLibrary the JavaScript code requires 4 parameters the FUNCTIONNAME is where the file content is returned. the other parameters builds a path like VideosLibrary\files\filename.txt, VideosLibrary\filename.txt or VideosLibrary\files\textfiles\filename.txt and so on.

Music Library

You can read ".txt" from MusicLibrary folder. Use the following javascript code to read these files.
The following code reads the file from MusicLibrary folder
jss.File.Read("FILENAME", "FUNCTIONNAME","MusicLibrary\\","\\");

The following code reads the filename from MusicLibrary sub folder files
jss.File.Read("FILENAME", "FUNCTIONNAME","MusicLibrary\\files","\\");

The following code reads the filename from MusicLibrary sub folder files\textfiles\
jss.File.Read("FILENAME", "FUNCTIONNAME","MusicLibrary\\files","\\textfiles\\");

Note that to read files from MusicLibrary the JavaScript code requires 4 parameters the FUNCTIONNAME is where the file content is returned. the other parameters builds a path like MusicLibrary\files\\filename.txt, MusicLibrary\filename.txt or MusicLibrary\files\textfiles\filename.txt and so on.

Storage Devices

You can read ".txt" from StorageDevice folder. Use the following javascript code to read these files.
The following code reads the file from StorageDevice folder
jss.File.Read("FILENAME", "FUNCTIONNAME","StorageDevice\\D:","\\");

The following code reads the filename from StorageDevice sub folder files
jss.File.Read("FILENAME", "FUNCTIONNAME","StorageDevice\\D:","\\files\\");

The following code reads the filename from StorageDevice sub folder files\textfiles\
jss.File.Read("FILENAME", "FUNCTIONNAME","StorageDevice\\D:","\\files\\textfiles\\");

Note that to read files from StorageDevice a Storage device name is required for example if you have 2 storage devices the first is "D:" The second is "E:" and so on. The JavaScript code requires 4 parameters the FUNCTIONNAME is where the file content is returned. the other parameters builds a path like D:\files\filename.txt, D:\filename.txt or D:\files\textfiles\filename.txt and so on.




File reading example

Load:jscriptstudio.js;
<p id="myfilecontent"></p>
<script>
  var jss = jscriptstudio;
  jss.File.Read("myfile.txt", "readMyFile");
  function readFiles() {
  document.getElementById("myfilecontent").innerHTML = readMyFile();
  }
</script>
<input type="button" onclick="readFiles()" value="Read Files" />

The code above is everything you need to read file from the app you are coding
The line Load:jscriptstudio.js; is required to load the library, in other words this is a library we have developed in the app to make it works once you call this code the library is included, if you don't add this line you won't be able to read files.

The line <p id="myfilecontent"></p> is where the file content will be placed but you can use any html tag you want.

The line var jss = jscriptstudio; is just to make the code short you can use jscriptstudio if you want instead of creating a variable.

The line jss.File.Read("myfile.txt", "readMyFile"); request the file myfile.txt when the file is found it returns the file content into a JavaScript function named readMyFile(), note that the function name returned is the same name you wrote on when you requested the file, the function returns a string content, in others words the syntaxis is jss.File.Read("FILENAME", "FUNCTIONNAME");

The line
  function readFiles() {
  document.getElementById("myfilecontent").innerHTML = readMyFile();
  }

Is a function we create to read all content from files, you can return the file content from any function you want, readMyFile() is the function returned when we request the file.

The line <input type="button" onclick="readFiles()" value="Read Files" /> is the input button where when we click it reads the file content.






About the author







Melvin Dev

This documentation has been written by Melvin Dev, Melvin Dev is a founder and developer of Arrowat LLC.


Follow Arrowat: https://arrowat.com/melvindev Facebook: https://www.facebook.com/melvindev Twitter: https://twitter.com/melvindevdotcom Instagram: https://www.instagram.com/melvindevdotcom Youtube: https://www.youtube.com/@melvindev LinkedIn: https://www.linkedin.com/in/melvindevdotcom