cancel
Showing results for 
Search instead for 
Did you mean: 

How can I read a text file inside the .xsjs?

Former Member
0 Kudos

I need to read line by line of a text file just like FileReader and BufferedReader in Java. It contains a script SQL to insert data in my DB. Can I use some API?

Accepted Solutions (1)

Accepted Solutions (1)

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

There is no API that allows you to access the file system of the server in XSJS. You would need to upload the file from the client side and it will appear as a array buffer in the request object.  From there you can convert the array buffer to a string and process however you like.

For example:

  var content = "";

    content = $.request.body.asArrayBuffer();

    var array = new Uint8Array(content);

    var encodedString = String.fromCharCode.apply(null,array),

         decodedString = decodeURIComponent(escape(encodedString));

        content = decodedString;

    var lines = content.split(/\r\n|\n/);

Former Member
0 Kudos

And what if you make a request to the file in the server side?

I mean, something like an XMLHttpRequest to the file-path-in-the-server.


I've done that from javascript, but I don't know if you can make it from a XSJS file, and I didn't needed to upload the file from the client side.

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

The file would have to be exposed via a Web Server. You can use an HTTP destination that just points to the local file system of the server.

yoppie_ariesthio
Participant
0 Kudos

This message was moderated.

Answers (0)