0

Just a quick question:

Is there a possibility to capture via JS the information from a prompt or form to a file, for example .txt? And thus save on the hard disk the information generated by my page through the information entered by the user.

It is for a scoreboard of a board game.

If this is not possible... is there any other option? I had thought that maybe I could print screen and save image by means of a JS event.

Thank you very much!

  • No it is no possible with javascript, you need a server-side environment like Node.js – Mina Aug 20 '22 at 06:43
  • The only option is to send your data to the backend using AJAX or submit the form. And then dump the content into a file in the backend. – Jagruti Aug 20 '22 at 06:47
  • Yes, you can generate a file and "download" it on the client browser - of course, you can't control where (or even if) the file is actually saved on the client computer - but you can definitely generate a "file" and try to download it, despite the above comments saying you require a server – Jaromanda X Aug 20 '22 at 07:01
  • You can suggest to the the user where to save the file but they will have complete control over where, and if, the file is actually saved so could you describe a bit more what the application requires? – A Haworth Aug 20 '22 at 09:44
  • Thank you for your answers. No problem, the page is for local use, I want to be able to save the data for security (accidental closing or refreshing of the page), and not lose the counts stored until then. Some sort of "log" where those results are recorded when an event is triggered (e.g. click on a new button) and the round is changed. I would love to be able to carry out this project in Node.js, but my knowledge is very basic, I don't know how to integrate my code to make it functional and enjoy the advantages of the backend. Of course, I will try to learn. – Paronirium Aug 20 '22 at 10:45
  • But to complete this project I would be grateful to the last users who have answered me to take into account the strictly personal use of my request. How could I pass this data to text, what commands or functions should I use? Thanks again – Paronirium Aug 20 '22 at 10:46

1 Answers1

0

You have two options here:

  1. Create a file and then ask the user to download it
  2. Use File System Access API to save the file

How to create a file or even better

Konrad
  • 21,590
  • 4
  • 28
  • 64
  • Thank you very much for your reply. I have visited the links and there is a lot of interesting information. However, I can't find about creating the file, its format and its content. The file should be created dynamically each round (it would be interesting to see a table with the players, the rounds, and the results inside; which is growing and saving each end of round), which would happen in a button click event. It would be downloaded by another button event, as long as the user triggers it. – Paronirium Aug 20 '22 at 11:56
  • First link google, but you can find thousand of blog post about it https://phpcoder.tech/create-dynamically-generated-text-file-and-download-using-javascript/ – Konrad Aug 20 '22 at 11:58