Moncader
Moncader

Reputation: 3388

Outputting a binary file from PHP as a Javascript binary string

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?

Upvotes: 1

Views: 1047

Answers (2)

chkn
chkn

Reputation: 717

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.

Upvotes: 1

Kyle Wild
Kyle Wild

Reputation: 8915

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

Upvotes: 0

Related Questions