Motivated
Motivated

Reputation: 163

Parsing text file contents using Javascript

I have a text file with the following content with each line representing a new object. The text file is stored locally on a PC e.g. C:\data.

Name,Role
PersonA,Administrator
PersonB,Engineer

As we are limited to using a session provided by a third party tool, we cannot use reference jQuery libraries or AJAX.

I would like the content to loop through as follows;

var p = {
            "PersonA" : "Administrator",
            "PersonB" : "Engineer"
        };

for (var key in p) 
{
    if (p.hasOwnProperty(key))
    {
        client = selectedPackage.Elements.AddNew(key, p[key]);
        client.Update();
    }
}

How do I reference a complete path e.g. c:\data\roles.txt and have its content loop through?

Upvotes: 0

Views: 74

Answers (2)

Oriol
Oriol

Reputation: 288120

JavaScript is limited, so

  • You can make AJAX connections, but only to the same domain (unless you use CORS).
  • You can't access files in client's machine, unless the client inputs them.

Then, I think the best solution is making the file accessible in your server, and get it using AJAX.

And if you say you can't use AJAX, maybe you could try using an input to load the file, and save it using localStorage for future uses.

Upvotes: 1

josh3736
josh3736

Reputation: 144912

In a browser? The only way to get access to a file on disk is by letting the user select the file with an <input type="file"> and then using the HTML5 File API to read its contents.

Upvotes: 1

Related Questions