Read text file and create dynamic variables

Zambetti, Luigi (Cognizant) 82 Reputation points
2022-01-01T10:19:50.223+00:00

Hello,
in my ASP.NET WebForms application I have a folder Scripts where there are
a JavaScript file and a TXT file.

This TXT file contains some values, that I can insert on it how I want.
For example, one value for row:

Value01
Value02
Value03
..

or in comma separated values:

Value01,Value02,Value03

Now my question is:

is it possible, in the JavaScript file, read this TXT file and create
as many variables as the values in the TXT file?

For example, in this case, something like this:

var myValue01 = Value01;
var myValue02 = Value02;
var myValue03 = Value03;

Thanks in advance for every idea.

Luis

Developer technologies | ASP.NET | Other
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2022-01-01T15:54:30.923+00:00

    Again, it is much easier to store the values in JSON (JavaScript Object Notation) format rather than a CSV string. Then you can can simply make an AJAX call to fetch the values without needing to do any manual serialization.

    Example using your requirement.

    public partial class _default : System.Web.UI.Page
    {
        public string Json { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateValuesFromFile();
        }
    
        protected void PopulateValuesFromFile()
        {
            List<string> Values = new List<string>();
            foreach (string line in System.IO.File.ReadLines(Server.MapPath("~/Javascript/values.txt")))
            {
                Values.AddRange(line.Split(','));
            }
    
            Json = Newtonsoft.Json.JsonConvert.SerializeObject(Values);
        }
    }
    

    Then within a script tag in the aspx page render the JavaScript.

    var values = <%=Json %>;
    
    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2022-01-02T13:33:37.867+00:00

    Again, I would use JSON rather than splitting a CSV string. Keep in mind that AJAX (fetch) is asynchronous. The code must wait for the HTTP request to finish before accessing the values.

    JSON file

    [
      "Value01",
      "Value02",
      "Value03"
    ]
    

    ASPX page using fetch within a script tag.

    fetch('https://localhost:44362/javascript/values.json')
        .then(response => response.json())
        .then(data => {
            console.log(data);
        });
    
    1 person found this answer helpful.
    0 comments No comments

  2. Ken Tucker 5,861 Reputation points
    2022-01-01T14:00:12.687+00:00

    I would use a javascript array to store mulitiple values from a text file

    https://www.w3schools.com/js/js_arrays.asp


  3. Bruce (SqlWork.com) 78,086 Reputation points Volunteer Moderator
    2022-01-01T18:06:05.133+00:00

    It matter little if you use an array or a mapped object. JavaScript can not read the text file. You have two options

    1) the webpage reads the text file and writes dynamic JavaScript to the page, as suggested in the first answer.

    2) the JavaScript makes an Ajax call to a page method that returns a json object, either a mapped object or array.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.