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?

2
  • More and completer information would greatly improve our answers to you. Commented Jan 17, 2011 at 7:26
  • Several reasons make your original plan very unpractical. These answers cover very much the same problem (json is javascript, but less): stackoverflow.com/questions/1443158/… Commented Jun 11, 2014 at 11:19

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The reason I'm not using AJAX is that it's not from the same domain :P
0

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

1 Comment

For speed I would prefer to avoid this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.