I have created an ‘assets’ folder in my p5.js sketch and enclosed a ‘xxxx.txt’ file. Does anyone know how to write data strings to the .txt file?
- Load your asset file using loadStrings(), which will return a string array.
- Make whatever changes to that array.
- When finished, save it back via saveStrings().
- However, the latter is gonna open a user file dialog!
I need to be able to write an array of strings repeatedly to the file without opening and editing it. Don’t want to have to use file dialog; just update the file (by code) every time I change the array.
For security reasons browsers don’t allow us to directly create or change local assets!
For that, we can request some server to save our files there via httpPost().
browsers don’t allow us to directly create or change local assets
Could explain why I’ve been having trouble. Thanks. Is there some public server where anyone can park data?
Unfortunately, I dunno much about client/server communication.
If you find a public server, probably you’re gonna need to write a server side code which would accept your sketch’s requests, I guess.
Alternate solution:
Is there a way to change the save location if I use createWriter(). My mac (Chrome browser) saves the files to my Download folder which makes it more difficult to re-upload them, in addition to the fact that I don’t know how to get the data back into my p5.js sketch without doing it manually.
I don’t think so. Not sure though.
You can request the user to do it via createFileInput() or drop(), which will get you a p5.File.
Apart from that, if you don’t have much to save, you can use storeItem().
Thanks for your help.