从 PHP 输出二进制文件作为 Javascript 二进制字符串
我有一个用 PHP 打开的二进制文件(类型无关紧要)。我希望能够将 PHP 中的数据输出到 Javascript 变量,即二进制字符串。为了比较和证明可能性,如果将 XmlHttpRequest 的覆盖 mime 类型设置为 text/plain 和用户定义的字符集,则可以强制将完整的二进制文件作为二进制字符串加载,然后在 javascript 世界中访问它。
我无法使用 ajax,因此需要能够直接从 PHP 脚本输出相同的数据。在正确转义所有与 javascript 相关的数据(例如引号、反斜杠和换行符)并将其对齐到 UTF-16 后,浏览器将在解析时抱怨字符串中存在非法字符。因此我错过了一个我需要逃脱的角色(可能)。有人知道它是什么吗?
I have a binary file (the type doesn't matter) that I have opened in PHP. I want to be able to output the data from PHP to a Javascript variable, ie, a binary string. For a comparison and proof of possibility, if you set a XmlHttpRequest's override mime type to text/plain and charset of user-defined, you can force the full binary file to be loaded as a binary string and then access that in javascript world.
I am unable to use ajax and therefore need to be able to output that same data directly from a PHP script. After correctly escaping all javascript related data such as quotes, backslashes and newlines as well as aligning it to UTF-16, The browser will complain about an illegal character inside the string while parsing. Therefore I have missed a character that I need to escape (probably). Anybody know what it is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 base64 或使用以下技术将数据嵌入 PNG:http://www.nihilogic。 dk/labs/canvascompress/。 PNG 技术将节省大量带宽,并且可以在任何具有 HTML5 画布的浏览器(所有现代浏览器,包括 IE9)中工作,只要 PNG 是从同一域提供的。
Either use base64 or embed the data in a PNG using a technique like at: http://www.nihilogic.dk/labs/canvascompress/. The PNG technique will save a lot of bandwidth and work in any browser with HTML5 canvas (all modern browers, including IE9), as long as the PNG is served from the same domain.
您可以尝试在 PHP 端对其进行 base64 编码,然后在 Javascript 中对其进行解码。
You could try base64 encoding it on the PHP side, then decoding it in Javascript.