如何备份html 5本地存储数据

时间:2021-11-07 17:16:57

am developing an offline application using html 5 and java script libraries. my system is for offline data collection. data is stored in the local machine in text format before being synced to the server later on. before i had developed the same application but using silver light which was easy to back up data for security reasons using

我正在使用html 5和java脚本库开发一个离线应用程序。我的系统用于离线数据收集。数据以文本格式存储在本地计算机中,然后再同步到服务器。在我开发相同的应用程序但使用银灯之前,出于安全原因使用容易备份数据

any idea on how i can backup the text files and zip them to my hard disk will be highly appreciated.

任何关于我如何备份文本文件并将其压缩到我的硬盘的想法将非常感激。

i have not found a solid answer through my research so far thanks in advance.

到目前为止,我还没有通过我的研究找到一个可靠的答案。

2 个解决方案

#1


3  

One "solution" I can think of is, before the user closes the app using the close button, you could detect the window.onbeforeunload event and send the data to your server to save. But this is not reliable because there might be cases when the browser may crash or the user might forcefully close the browser application. Or worst of all if you are constantly using localStorage and by chance the user clears the browsing data (which includes localStorage) it will be gone.

我能想到的一个“解决方案”是,在用户使用关闭按钮关闭应用程序之前,您可以检测到window.onbeforeunload事件并将数据发送到服务器进行保存。但这不可靠,因为可能存在浏览器崩溃或用户可能强行关闭浏览器应用程序的情况。或者最糟糕的是,如果你经常使用localStorage,并且用户清除浏览数据(包括localStorage),它将会消失。

But assuming you have the data at hand, you can send it to the server using POST and make a script to save it in the disk. Obviously, you might want to impose limitation on file size and enforce other security restrictions.

但假设您掌握了数据,可以使用POST将其发送到服务器并制作脚本以将其保存在磁盘中。显然,您可能希望对文件大小施加限制并强制执行其他安全限制。

WARNING: PHP

Lets say you have a folder created for each unique user and all files in each user's folder are guaranteed to be named uniquely. In PHP you can use functions like file_put_contents() to save a text file, you can also easily ZIP it using the ZipArchive class.

假设您为每个唯一用户创建了一个文件夹,并且每个用户文件夹中的所有文件都保证唯一命名。在PHP中,您可以使用file_put_contents()等函数来保存文本文件,也可以使用ZipArchive类轻松地将其压缩。

It's not recommended to store this kind of data directly to the drive. I would highly recommend you to put the localStorage data in some kind of database and do a database backup instead of backing up individual user's data.

建议不要将此类数据直接存储到驱动器中。我强烈建议您将localStorage数据放在某种数据库中并进行数据库备份,而不是备份单个用户的数据。

As other users have pointed it out you could also look at the HTML5 File API. Examples here.

正如其他用户指出的那样,您也可以查看HTML5文件API。这里的例子。

#2


1  

I found a solution on how to backup local storage files and zip them to my local drive without any server code. Below is a code snippet that works fine for me at least for now. Any modifications and enhancements 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


3  

One "solution" I can think of is, before the user closes the app using the close button, you could detect the window.onbeforeunload event and send the data to your server to save. But this is not reliable because there might be cases when the browser may crash or the user might forcefully close the browser application. Or worst of all if you are constantly using localStorage and by chance the user clears the browsing data (which includes localStorage) it will be gone.

我能想到的一个“解决方案”是,在用户使用关闭按钮关闭应用程序之前,您可以检测到window.onbeforeunload事件并将数据发送到服务器进行保存。但这不可靠,因为可能存在浏览器崩溃或用户可能强行关闭浏览器应用程序的情况。或者最糟糕的是,如果你经常使用localStorage,并且用户清除浏览数据(包括localStorage),它将会消失。

But assuming you have the data at hand, you can send it to the server using POST and make a script to save it in the disk. Obviously, you might want to impose limitation on file size and enforce other security restrictions.

但假设您掌握了数据,可以使用POST将其发送到服务器并制作脚本以将其保存在磁盘中。显然,您可能希望对文件大小施加限制并强制执行其他安全限制。

WARNING: PHP

Lets say you have a folder created for each unique user and all files in each user's folder are guaranteed to be named uniquely. In PHP you can use functions like file_put_contents() to save a text file, you can also easily ZIP it using the ZipArchive class.

假设您为每个唯一用户创建了一个文件夹,并且每个用户文件夹中的所有文件都保证唯一命名。在PHP中,您可以使用file_put_contents()等函数来保存文本文件,也可以使用ZipArchive类轻松地将其压缩。

It's not recommended to store this kind of data directly to the drive. I would highly recommend you to put the localStorage data in some kind of database and do a database backup instead of backing up individual user's data.

建议不要将此类数据直接存储到驱动器中。我强烈建议您将localStorage数据放在某种数据库中并进行数据库备份,而不是备份单个用户的数据。

As other users have pointed it out you could also look at the HTML5 File API. Examples here.

正如其他用户指出的那样,您也可以查看HTML5文件API。这里的例子。

#2


1  

I found a solution on how to backup local storage files and zip them to my local drive without any server code. Below is a code snippet that works fine for me at least for now. Any modifications and enhancements 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" }));

    }
}