0

Since Mozilla's btoa and atob aren't compatible with IE, Im using Nick Galbreath's solution that works across the board.

In my JS, I have this snippet:

reader.onload = function (e)
{
    var base64str = e.target.result.split(';')[1].split(',')[1];
    var binaryData = base64.decode(base64str); 
    
    // binaryData looks like: 3!1AQa"q2¡±B#$RÁb34rÑC%Sðáñcs5¢²&DTdE£t
    // 6ÒUâeò³ÃÓuãóF'¤´ÄÔäô¥µÅÕåõVfv¦¶ÆÖæö7GWgw§·Ç×ç÷5!1AQaq"2¡±B#ÁRÑð
    // 3$bárCScs4ñ%¢²&5ÂÒDT£dEU6teâò³ÃÓuãóF¤´ÄÔäô¥µÅÕåõVfv¦¶ÆÖæö'7GWgw
    // §·ÇÿÚ?õTI%)$IJI$RIrÿ[múÙxÝ^«ÝKØrþk²ïÑûíGóß÷¿ÑþÄY«ÍÓ±×úN //...
    // Is this even binary data?

    Ajax.SendToHandler(binaryData);
}

How do I convert binaryData, which is sent to my ashx derived IHttpHandler as a string, into a bytes[] array?

Ask me to clarify where needed!

10
  • What is binary string ? What is the reason for tagging it c#? Commented May 16, 2013 at 21:39
  • I say binary string, because the atob function, or base64.decode should convert ascii to binary (a to b). binaryData holds the result of the decoding, which is in my snippet above, which I called the 'binary string'. It is very likely I'm using the wrong terminology. Commented May 16, 2013 at 21:41
  • I tagged c# because it goes to my ashx handler, where I need to convert the binaryData from HttpContext['binaryData'] into a byte[] array. Commented May 16, 2013 at 21:43
  • show us your binarydata Commented May 16, 2013 at 21:46
  • 2
    Why not sending the base64 string as it is and decode it on the server? Base64 is exactly for sending binary data as text. Commented May 16, 2013 at 21:59

1 Answer 1

2

Your data string seems to contain only extended ASCII characters (probably either Windows-1252 characters or ISO 8859-1 characters). You should try using a System.Text.Encoding to convert it to bytes.

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

8 Comments

So I actually did that using System.Text.Encoding.ASCII.GetBytes. However, to do a sanity check, I convert it to base 64 in the handler and see if the result is the same on the back-end as it is on the front-end before its decoded, and they're not the same. Thoughts?
Encoding.ASCII is no good due to being limited to 0-127. Try Encoding.Default or Encoding.GetEncoding("iso-8859-1").
You, my friend, are a god-send. May I ask how you knew what type of encoding to use?
The letters are mostly accented letters, but nothing that looks overtly non-Latin (such as japanese, cyrillic, etc.)
Let me ask you this, is the encoding browser dependent? Will the encoding be different for users on different browsers on different machines?
|

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.