加载非托管HTML5应用程序的外部文件 [英] Loading external files for a non-hosted HTML5 application

查看:97
本文介绍了加载非托管HTML5应用程序的外部文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一款加载外部资源的HTML5游戏。目前,我正在使用XMLHttpRequest读取文件,但这不适用于Chrome,导致

I am currently developing a HTML5 game which loads in an external resource. Currently, I am using XMLHttpRequest to read in the file, but this does not work on Chrome, resulting in a

XMLHttpRequest cannot load file:///E:/game/data.txt
Cross origin requests are only supported for HTTP.

该文件与HTML5文件位于同一目录中。

The file is in the same directory as the HTML5 file.

问题:


  1. HTML5应用程序是否有办法使用XMLHttpRequest(或
    )其他方法)加载外部文件而不需要在网络服务器上托管

  1. Is there any way for a HTML5 application to use XMLHttpRequest (or other method) to load in an external file without requiring it to be hosted on a webserver?

如果我将HTML5代码打包为一个应用程序在支持HTML5的平板电脑/手机
上,XMLHttpRequest能否加载外部
文件?

If I package the HTML5 code as an application on a tablet/phone which supports HTML5, would XMLHttpRequest be able to load external files?


推荐答案


  1. (a)是和否。作为安全策略的问题,XHR传统上都是相同的协议(即:http://,而不是file:///),并且最重要的是,传统上也是同一个域(如:在相同的子域 - http://pages.site.com/index 无法从 http://scripts.site获取文件。 COM / )。跨域请求现在可用,但无论如何都需要Web服务器,托管该文件的服务器必须专门接受该请求。

  1. (a) Yes and no. As a matter of security-policy, XHR has traditionally been both same-protocol (ie: http://, rather than file:///), and on top of that, has traditionally been same-domain, as well (as in same subdomain -- http://pages.site.com/index can't get a file from http://scripts.site.com/). Cross-domain requests are now available, but they require a webserver, regardless, and the server hosting the file has to accept the request specifically.

(b)所以在迂回的方式,答案是肯定的,一些实现可能(错误地)允许您通过XHR获取文件,即使页面是以文件系统术语发言,而不是http请求(旧版本的浏览器)......但是,否则你将需要一种或那种网络服务器。好消息是它们很容易安装。 EasyPHP就足够了,它几乎是一个3点击的解决方案。还有无数其他人。如果你想要的只是一个apache中的文件服务器,并且你不打算使用服务器端脚本语言(或者如果你打算使用它),这只是脑关闭安装方面首先想到的。 PHP)。

(b) So in a roundabout way, the answer is yes, some implementations might (incorrectly) allow you to grab a file through XHR, even if the page is speaking in file-system terms, rather than http requests (older versions of browsers)... ...but otherwise you're going to need a webserver of one kind or another. The good news is that they're dirt-simple to install. EasyPHP would be sufficient, and it's pretty much a 3-click solution. There are countless others as well. It's just the first that comes to mind in terms of brain-off installation, if all you want is a file-server in apache, and you aren't planning on using a server-side scripting language (or if you do plan on using PHP).

XMLHttpRequest绝对能够获取外部文件...

IF 它们实际上是外部的(即:未捆绑在手机专用缓存中 - 使用手机的内置文件访问API,并编写一个包装器来处理每个具有相同的自定义界面), 手机目前有接收 - 准备好处理故障情况(比如有一个默认设置对象,或有错误处理或任何最好的 - 案例是,对于任何遗漏的东西)。

XMLHttpRequest would absolutely be able to get external files...
IF they're actually external (ie: not bundled in a phone-specific cache -- use the phone's built-in file-access API for that, and write a wrapper to handle each one with the same, custom interface), AND the phone currently has reception -- be prepared to handle failure-conditions (like having a default-settings object, or having error-handling or whatever the best-case is, for whatever is missing).

另外,查看Application Cache Manifests。同样,这是一个html5解决方案,不同版本的不同手机处理方式不同(早期与更标准化的格式)。 在开发过程中不要使用它,因为它会使代码/内容更改变得缓慢而痛苦 ,但是当您的产品完成并且没有错误时,它非常有用,并且距离发布几秒钟,您可以告诉用户的浏览器永久缓存所有内容,以便他们可以脱机播放,并且可以节省各种带宽,而不必在下次播放时下载所有内容。

Also, look into Application Cache Manifests. Again, this is an html5 solution which different versions of different phones handle differently (early-days versus more standardized formats). DO NOT USE IT DURING DEVELOPMENT, AS IT MAKES TESTING CODE/CONTENT CHANGES MISERABLY SLOW AND PAINFUL, but it's useful when your product is pretty much finished and bug-free, and seconds away from launch, where you tell users' browsers to cache all of the content for eternity, so that they can play offline, and they can save all kinds of bandwidth, not having to download everything the next time they play.

这篇关于加载非托管HTML5应用程序的外部文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆