moxie icon indicating copy to clipboard operation
moxie copied to clipboard

Add readAsArrayBuffer and drop support for readAsBinaryString

Open eric1234 opened this issue 11 years ago • 12 comments

It appears that readAsBinaryString is no longer supported by the HTML5 spec. Although implemented in some browsers (Chrome, Firefox) it will likely be removed in future releases. In addition IE10 was released late enough that they never needed to support it (but they do support readAsArrayBuffer and readAsText and readAsDataURL).

The end result of the current situation is that:

  • If I use readAsBinaryString it seems to work most places. But I get an error in IE10 (complaining about the method not existing). I imagine future version of Firefox and Webkit will behave the same.
  • If I use readAsArrayBuffer it will work on all modern browsers (including IE10) but it will not work on browsers using the flash fallback since the flash fallback doesn't provide support for readAsArrayBuffer.
  • readAsText should work everywhere. But if you plan to use interact with the data you might get issues since it will apply character encoding mojo. I don't need to interact with the data in a meaningful way (just doing a hash of some of the data) so I will probably try this one.
  • readAsDataURL also should work everywhere. But since it is base64 encoded I think it will increase the file size by 37% which could affect performance.

eric1234 avatar Jul 17 '13 19:07 eric1234

Just a quick followup related to this I figured I would note. I tried readAsText and the data returned was "nil" when using the flash fallback (IE9). But when using readAsDataURL I got what I needed on all browsers. Wasn't sure if the nil was due to the fact that I was reading binary data.

eric1234 avatar Jul 17 '13 20:07 eric1234

Same issue here, we would like to see readAsArrayBuffer implemented if possible. Are there any workarounds to apply in the meantime?

gixo avatar Jul 23 '13 09:07 gixo

Adding support for readAsArrayBuffer will require dramatic alterations here and there. I will definitely come to this one sooner or later, but not for current release.

jayarjo avatar Jul 23 '13 20:07 jayarjo

This will do for now, thanks! :)

gixo avatar Jul 24 '13 22:07 gixo

no offence guys but i tried readAsArrayBuffer on IE 10.0.9200.16736 and isn't working

adcorduneanu avatar Dec 12 '13 09:12 adcorduneanu

readAsArrayBuffer is not implemented yet, the temp solution was to emulate readAsBinaryString where it is not present.

jayarjo avatar Dec 12 '13 11:12 jayarjo

Any progress on this? Is there an expected release date for this functionality?

bcbirkhauser avatar Jun 07 '14 20:06 bcbirkhauser

Any updates on this?

andy250 avatar Dec 09 '16 15:12 andy250

@andy250 can you describe in more detail how do you plan to use it?

readAsArrayBuffer is pointless to implement in runtimes other than HTML5 (will be slow and unusable). So the logic behind not implementing it was that if you plan to use it then you already plan to work in the environment that doesn't require moxie altogether.

jayarjo avatar Dec 10 '16 04:12 jayarjo

@jayarjo I planned to use this method to read file chunk into a byte array (raw bytes from disk). Then feed this byte array to spark-md5 to calculate its md5 hash. I got it working already using browser's native FileReader and readAsArrayBuffer. The md5 hash matches the one calculated on the server. When using moxie FileReader's readAsText or readAsBinaryString I am getting different hashes (I suppose encoding issues). So my primary goal is to get raw bytes of a file chunk from disk.

I suppose there is some workaround to achieve this in other runtimes. I already thought about converting the string I got from readAsText into byte array, but again I need to know the encoding. Any suggestsions? If there is no way to achieve what I need I guess my best shot is to support md5 hashes only in html5 runtime.

BTW: why there is a problem to read raw file bytes e.g. in flash?

andy250 avatar Dec 10 '16 19:12 andy250

Flash doesn't allow direct access to file source until you preload the whole file in memory, which is not desirable operation in most cases. And then there's a need to pipe those bytes to and from the runtime, which basically kills all the benefit of using ArrayBuffer in first place.

You can always get hold of the native browser file object by doing file.getSource() on one produced by moxie.

jayarjo avatar Dec 11 '16 07:12 jayarjo

@jayarjo Thanks. But even if I use file.getSource, then (when runtime is flash) this must be the whole file, right? So I cannot really chunk it. And how does it look in silverlight? Same restrictions and no point in using the ArrayBuffer?

andy250 avatar Dec 11 '16 18:12 andy250