3

In the HTML5, it introduced the FileReader API. I can't really understand the difference between readAsBinaryString() and readAsDataURL(). I read docs from several places, but still can't fully understand. Can someone give some code examples to help me understand the differences?

user926958
  • 9,355
  • 7
  • 28
  • 33

1 Answers1

12

If you use readAsDataURL(), you get the data back in a data URI format. So something like the src attribute here:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">

If you use readAsBinaryString(), the result will be the raw bits. You can then use one of the low level binary APIs to manipulate the data or post it to the server.

PaulMest
  • 12,925
  • 7
  • 53
  • 50
Maurice
  • 27,582
  • 5
  • 49
  • 62