How to read and write files with javascript? | XM Community
Skip to main content
Hi all,

I want to read and write files with javascript. The file might be stored in javascript files library, or in a remote server.



What I am looking for is to read and update some values when each participant start the survey. These are global values, so embedded data will not work.



Thanks for your help.
I think this is going to be particularly relevant for you in this case: https://stackoverflow.com/questions/21012580/is-it-possible-to-write-data-to-file-using-only-javascript



However you are definitely going to encounter issues writing to remote servers without an api on that server for doing so.
Hello @rakib062 ,



You can read files with js but make sure that you do that part in the "look and feel" -> "Advanced" -> "Header"(edit)-> "<>" HTML view inside "`<script>`" tag.



Since this header script will run every time when new page is loaded in the survey and data can be accessible throughout the survey.



Following code will fetch the data of file(present on the same server i.e in qualtrics files library)

`jQuery.get('//File URL', function(result) {console.log("File Contains -"+result);});`





If you are using remote file for read and write operation, then you need to have an api at server side i.e where remote file is hosted (as mentioned by @AnthonyR ) and then using web-service element of survey flow you can read or write the data in remote file.
Thanks Shashi. Could you please provide an example of what the 'File URL' will look like if the file is save in qualtrics library? And can it be edited with javascript?
> @rakib062 said:

> Thanks Shashi. Could you please provide an example of what the 'File URL' will look like if the file is save in qualtrics library? And can it be edited with javascript?



The URL will be like the following:

`https://your_data_center_id/ControlPanel/File.php?F=F_0qFq48an1FIddb`



You can read the data in file but there is no way to edit and upload it again in files library
Thank you for an useful discussion. I also want to read some data from file. I've managed to do it somehow, but I have no idea how to save the responseText only into a string variable. Could anyone help me?



I've tried it partially using answer by Shashi:

`var output = jQuery.get('file_address, function(result) {result;});`

Then I've stored it into an embedded data field, since I have no idea where to find the console log. I got the output in following form:

`{"readyState":4,"responseText":"name1,el1a,el1b,el1c\\nname2,el2a,el2b,el2c\\nname3,el3a,el3b,el3c\\n","status":200,"statusText":"OK"} `



I've tried to store the responseText only by using

`var output = jQuery.get('file_address', function() {this.responseText;});`

or

`var output = jQuery.get('file_address', function(result) {result.responseText;});`

but nothing has changed.



I've tried also to extract the responseText using regex or to modify function using information from other discussions (e.g.: here, here or here). In these cases nothing appeared in my embedded data field.

Thank you very much for any ideas how to solve this.
Hello @fleb ,



Your code will also work, just make sure the file is in your qualtrics files library

Create an embedded data (data) as first element in survey flow, and paste the below code in header inside script tag

`<script>

var rdata="${e://Field/data}";

if(rdata.length <= 0 ){

jQuery.get("YOUR_LINK", function(result){Qualtrics.SurveyEngine.setEmbeddedData("data",result); }); }

</script>`
Hi @Shashi,

thanks a lot for your response. My data are in the field data and it seems they are in the correct form now. I'm able to display these data in the questionnaire, but I can't access them from the` Qualtrics.SurveyEngine.addOnload`. I would like to convert the text to an array and use it (together with some responses obtained by the questionnaire) to compute some properties of the respondent and show these results to him/her.

To evaluate whether I can access the filed "data" from the ` Qualtrics.SurveyEngine.addOnload`I tried to copy the content to next embedded data field (defined also at the beginning of the flow). However this didn't work.

The code was following:

Leave a Reply