简体   繁体   English

如何通过Ajax将下载的文件保留在内存中

[英]How to keep downloaded file in memory through ajax

I am making a html5 web app based on an existing ios app. 我正在基于现有的ios应用制作html5网络应用。 It targets most recent two versions of ios Safari browser. 它以ios Safari浏览器的最新两个版本为目标。 For this web app to work, I need to use an api that handles communication between current ios app and php backend. 为了使此Web应用程序正常工作,我需要使用一个处理当前ios应用程序与php后端之间通信的api。

The very first hurdle is that api config files (gzipped) needs to be downloaded from server on a regular basis (for example each time app starts) to keep itself updated. 第一个障碍是需要定期(例如,每次应用启动时)从服务器下载api配置文件(已压缩)(保持压缩)以保持自身更新。 Then I need to unzip to get config files, which basically contains key-value pairs of command name and http address. 然后,我需要解压缩以获取配置文件,该文件基本上包含命令名称和http地址的键值对。 When I need certain command, eg display a user's profile pic, I lookup the config files by command number to find the corresponding request address so that I could make request to server. 当我需要某些命令(例如显示用户的个人资料图片)时,我可以通过命令编号查找配置文件以找到相应的请求地址,以便可以向服务器发出请求。

To summerize the steps: 要总结这些步骤:

  1. Download (gziped) configuration files by ajax 通过ajax下载(压缩)配置文件

    I don't think html anchor tag (with or without download attribute) is an option. 我不认为html锚标记(带有或不带有下载属性)不是一种选择。 Because in that way Safari will prompt user to open the downloaded file using file handling apps (eg FilesApp). 因为以这种方式,Safari会提示用户使用文件处理应用程序(例如FilesApp)打开下载的文件。 The download must happen in the dark. 下载必须在黑暗中进行。

  2. Unzip the downloaded file 解压下载的文件

    I know a few js library promise to do this work. 我知道一些js库有望完成这项工作。 I haven't tested them yet for step one is not solved. 我还没有测试它们,因为第一步没有解决。 Any recommandatio is welcomed. 欢迎您提出任何建议。

  3. Lookup http address in those files whenever making a api call 每当进行api调用时,在这些文件中查找http地址

    It should be no problem though. 不过应该没问题。

For ios app, I can simply download the config files into app sandbox and do whatever I want with it. 对于ios应用程序,我可以简单地将配置文件下载到应用程序沙箱中,然后用它做任何我想做的事情。 But for web app, even newest ios Safari don't suppor HTML5 file system api. 但是对于Web应用程序,即使是最新的ios Safari也不支持HTML5文件系统api。 What can I do? 我能做什么?

Step 1. 步骤1。

Safari should support HTTP compression. Safari应该支持HTTP压缩。 Your best best is to leverage this, as opposed to figuring out a way to unzip a file using JavaScript. 最好的办法是利用此功能,而不是想办法使用JavaScript解压缩文件。 It looks like there are a few options with PHP: 看来PHP有一些选择:

Or even at the server level: 甚至在服务器级别:

Step 2. 第2步。

Use sessionStorage to cache the configuration settings. 使用sessionStorage缓存配置设置。 The data will be cleared out each time a user closes their browser. 每次用户关闭浏览器时,数据将被清除。

Step 3. 第三步

The PHP handler that is called by the AJAX request should take care of parsing the config files and send JavaScript-friendly JSON to the browser. 由AJAX请求调用的PHP处理程序应注意解析配置文件,并将JavaScript友好的JSON发送到浏览器。 Use JSON.parse() and JSON.stringify() to serialize/de-serialize data as you read/save to sessionStorage . 在读取/保存到sessionStorage使用JSON.parse()JSON.stringify()对数据进行序列化/反序列化。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM