How to backup html 5 local storage data

am developing a standalone application using html 5 and java script libraries. My system is designed for offline data collection. data is saved on the local computer in text format before being synchronized with the server later. before I developed the same application, but used silver light, which was easy to back up data for security reasons, using

Any idea of โ€‹โ€‹how I can back up text files and zip them onto my hard drive will be much appreciated.

I have not found a reliable answer in my research so far thanks in advance.

+4
source share
2 answers

One โ€œsolutionโ€ that I can think of is that before the user closes the application with the close button, you can detect the window.onbeforeunload event and send the data to your server for saving. But this is unreliable, because there may be times when the browser may crash or the user may force close the browser application. Or, worst of all, if you constantly use localStorage, and, by chance, the user clears the browser data (including localStorage), it will disappear.

But if you have data, you can send it to the server using POST and make a script to save it to disk. Obviously, you can impose a file size limit and apply other security restrictions.

WARNING: PHP

Suppose you have a folder created for each unique user, and all files in each user folder are guaranteed to be called unique. In PHP you can use functions like file_put_contents() to save a text file, you can also easily zip it with the ZipArchive class.

It is not recommended to store this type of data directly on disk. I highly recommend that you put localStorage data in some kind of database and back up the database instead of backing up individual user data.

As other users have pointed out, you can also look at the HTML5 API files. Examples here .

+3
source

I found a solution on how to back up local storage files and encrypt them on a local drive without server code. Below is a snippet of code that works for me at least for now. Any changes and improvements will be highly appreciated.

 if (localStorageKeys.length > 0) { for (var i = 0; i < localStorageKeys.length; i++) { var key = localStorageKeys[i]; if (key.search(_instrumentId) != -1) { keysToBackup.push(key); var data = localStorage.getItem(localStorageKeys[i]); zip.file(localStorageKeys[i].slice(0, -19) + ".txt", data); } else { keysNotToBackup.push(key); } var datafile = document.getElementById('backupData'); datafile.download = formName + "_Data.zip"; datafile.href = window.URL.createObjectURL(zip.generate({ type: "blob" })); } } 
+1
source

Source: https://habr.com/ru/post/1491061/


All Articles