简体   繁体   English

是否可以从HTML5的本地存储中提取数据并保存到服务器数据库?

[英]Is it possible to pull data from HTML5's local storage and save to server database?

I have an idea for a social web app and part of the idea is to pull data from localstorage onto the server database. 我有一个社交网络应用程序的想法,部分想法是将数据从localstorage提取到服务器数据库。 The reason for this is say you have an idea and you want to put it on your next status for this social app, but you have no signal and wireless anywhere. 这样做的原因是你有一个想法,你想把它放在你的社交应用程序的下一个状态,但你没有任何信号和无线。 You write out the status on the app and it saves it to the localstorage for use when they get back to somewhere where they can connect their phone online. 您在应用程序上写下状态,并将其保存到本地存储区,以便在他们返回可以在线连接手机的地方时使用。

They then get to a place where they can go online, the app then detects that there is a change in the localstorage and then pulls it from there and saves to the server database. 然后他们到达一个他们可以上网的地方,应用程序然后检测到localstorage中有一个更改,然后从那里拉出它并保存到服务器数据库。

Is this at all possible, if so how would you go about implementing it? 这是否可能,如果是这样,您将如何实施它? If it's not at all possible, do you think this could be sometime in the future? 如果不可能的话,你认为这可能是将来的某个时候吗?

I look forward to hearing from your comments and answers on this one as I think it could be quite a big discussion. 我期待听到您对此问题的评论和答案,因为我认为这可能是一次非常大的讨论。

Yes it's possible, why wouldn't it be? 是的,有可能,为什么不呢? When you pull data from the local storage, the data works just like any other Javascript variable, there are no restrictions that would stop you sending it to a server other than the lack of Internet connection. 当您从本地存储中提取数据时,数据就像任何其他Javascript变量一样,没有任何限制会阻止您将其发送到除缺少Internet连接之外的服务器。

how would you go about implementing it? 你将如何实施它?

First you'd need to detect the connection status, see this question . 首先,您需要检测连接状态,请参阅此问题 So when a user tries to update their status, it checks if the connection is online and if it isn't then save it to local storage. 因此,当用户尝试更新其状态时,它会检查连接是否在线,如果不是,则将其保存到本地存储。

Use the methods in the linked question to detect when the connection comes back up, and at that point pull the data from local storage and send it to the server via , then clear the local storage to prevent duplicate data being sent. 使用链接问题中的方法检测连接何时恢复,然后从本地存储中提取数据并通过将其发送到服务器,然后清除本地存储以防止发送重复数据。

You can periodically check navigator.onLine in your Javascript to get the online status of a device. 您可以定期检查Javascript中的navigator.onLine以获取设备的在线状态。 Note that while 请注意,虽然

  navigator.onLine == false 

safely tells you that a device of offline, 安全地告诉你一个离线的设备,

 navigator.onLine == true 

doesn't necessarily mean it has access to the internet and can perform the request, just that it is connected to some sort of network. 并不一定意味着它可以访问互联网并且可以执行请求,只是它连接到某种网络。

Once you assume that the device is not offline you'd send an ajax request (I recommend using jQuery's $.ajax function) in which you POST the data from your localStorage 一旦你认为设备没有脱机,你就会发送一个ajax请求(我建议使用jQuery的$ .ajax函数),你可以从localStorage中发布数据

(localStorage.getItem('dataToPull'))

to a PHP script, which then inserts it into your MySQL database. 到PHP脚本,然后将其插入MySQL数据库。

If that ajax request fails, and you're sure it's not getting a PHP/MySQL error, it's safe to assume that although the device is connected to a network and navigator.onLine is true, there's no internet connectivity. 如果该ajax请求失败,并且您确定它没有得到PHP / MySQL错误,则可以安全地假设虽然该设备已连接到网络并且navigator.onLine为true,但是没有互联网连接。 You can then do some error handling, eg poll the device again in 5 minutes. 然后,您可以进行一些错误处理,例如在5分钟内再次轮询设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以仅使用HTML5本地数据库而不使用Mysql从excel或CSV文件中的数据进行收集? - Is it possible to collect from data in excel or CSV file using HTML5 Local database only, without using Mysql? 使用HTML5本地存储镜像MySQL数据库架构以进行查询 - Mirror MySQL Database Schema with HTML5 local storage for querying 如何使用PHP将HTML5数据属性保存到数据库? - How to save HTML5 data- attribute to database using PHP? HTML5 CANVAS:如何从服务器保存并重新打开图像 - HTML5 CANVAS: How to save and reopen image from server 通过HTML5验证使用Javascript检查表单是否有效,并将表单数据存储在本地存储中 - Check using Javascript if form is valid by HTML5 validation and store form data in local storage 将数据从数据库保存到 <option>在HTML中 - Save data from database to <option> in HTML 从数据库中获取数据值以保存到 session 存储? - Getting Data values from database to save to session storage? 将HTML5画布保存到服务器上的文件夹 - Save a HTML5 canvas to a folder on a server 如何将 HTML5 Canvas 保存为服务器上的图像? - How to save an HTML5 Canvas as an image on a server? 我们能否在curl请求上存储使用html5本地存储 - Can we store use html5 local storage on curl request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM