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!

Community
  • 1
  • 1
Josh
  • 1,019
  • 3
  • 17
  • 32
  • What is `binary string` ? What is the reason for tagging it c#? – I4V May 16 '13 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. – Josh May 16 '13 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. – Josh May 16 '13 at 21:43
  • show us your binarydata – muratgu May 16 '13 at 21:46
  • It's large to post here, but a very small portion is in the snippet above. Do you need more? – Josh May 16 '13 at 21:47
  • 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. – Tz_ May 16 '13 at 21:59
  • Besides that it adds 33% inflation to the filesize, I'm required not to do so. There can be up to 25 images getting uploaded at one time, so we'd like to minimize the wait time as much as possible. Your thoughts? – Josh May 16 '13 at 22:02
  • http://stackoverflow.com/questions/1443158/binary-data-in-json-string-something-better-than-base64 - there are a lot of ideas about compressing base64 data. Maybe it helps? – Tz_ May 16 '13 at 22:17
  • @Josh When uploading files over HTTP, doesn't the binary data get MIME-encoded anyway? (Or something like that. I'm not really sure what all the variables in this setup are.) – millimoose May 16 '13 at 22:26
  • I can't say whether it does or doesn't, since I'm not as familiar as I'm trying to be with it. – Josh May 16 '13 at 22:30

1 Answers1

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.

Medinoc
  • 6,577
  • 20
  • 42
  • 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? – Josh May 16 '13 at 22:23
  • 1
    `Encoding.ASCII` is no good due to being limited to 0-127. Try `Encoding.Default` or `Encoding.GetEncoding("iso-8859-1")`. – Medinoc May 16 '13 at 22:29
  • You, my friend, are a god-send. May I ask how you knew what type of encoding to use? – Josh May 16 '13 at 22:43
  • The letters are mostly accented letters, but nothing that looks overtly non-Latin (such as japanese, cyrillic, etc.) – Medinoc May 16 '13 at 22:51
  • Let me ask you this, is the encoding browser dependent? Will the encoding be different for users on different browsers on different machines? – Josh May 16 '13 at 22:52
  • This is outside my competences, sorry. – Medinoc May 16 '13 at 22:56
  • Do you know how to escape/unescape the binary string in the handler to avoid `System.Web.HttpRequestValidationException`? – Josh May 17 '13 at 14:17
  • Nope, sorry. Usually I just use base64 wherever possible. – Medinoc May 17 '13 at 16:39