1

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?

Moncader
  • 3,388
  • 3
  • 22
  • 28
  • More and completer information would greatly improve our answers to you. – mplungjan Jan 17 '11 at 07:26
  • Several reasons make your original plan very unpractical. These answers cover very much the same problem (json is javascript, but less): http://stackoverflow.com/questions/1443158/binary-data-in-json-string-something-better-than-base64 – Simon B. Jun 11 '14 at 11:19

2 Answers2

1

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.

chkn
  • 647
  • 7
  • 14
0

You could try base64 encoding it on the PHP side, then decoding it in Javascript.

Kyle Wild
  • 8,845
  • 2
  • 36
  • 36