-1

I'm developing a web app using ReactJS and trying to create a txt file with a number in it on the local storage, c:\temp for example.

Is it possible to do it without keep asking the user for his approval (dialog)?

Thanks

user1630359
  • 103
  • 8
  • 1
    No, as far as I know, the only way to write a file to the disk is to download it, and it requires user's approval. Browsers would never allow JS to silently write a file to the disk, for obvious security reasons. – Jeremy Thille Jun 02 '21 at 13:40
  • You can't store directly on a users drive, but you can use the File & Directory API, this creates a virtual file system were you could store text files without constantly asking the user. More info here -> https://developer.mozilla.org/en-US/docs/Web/API/File_and_Directory_Entries_API/Introduction Of course if it's just txt files you might find localeStorage will be just as good, but the File / Directory API gives you that classic File / Directory feel.. :) – Keith Jun 02 '21 at 13:54

1 Answers1

1

localStorage is a browser API and not an arbitrary file on the user's disk. If you are going to use it, then there is a handy React hook for it.

You can't write to arbitrary files on the user's disk, although you can generate a download from in-memory data. This may be saved to the user's download folder or may prompt a SaveAs dialog (you can't control which).

If you want to store data on the server then you can make HTTP requests to it (i.e. use Ajax) and write a web service to process those requests.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335