You develop a webpage that allows a user to download a JPEG image and convert it to a PNG file.
You need to implement the code to download the contents of the JPEG image with no additional decoding.
Which JavaScript function should you use?

A.
readAsBinaryString()
B.
readAsArrayBuffer()
C.
readAsDataURL()
D.
readAsText()
Explanation:
The readAsArrayBuffer method is used to read a File, Blob, MSStream into memory as an ArrayBuffer object.
FileReader.readAsArrayBuffer
B is ok.
5
0
B is correct.
From stackoverflow:
.readAsDataURL() return a URL representing the file’s data as a base64 encoded string
.readAsArrayBuffer() return an ArrayBuffer representing the file’s data
.readAsText() return the file’s data as a text string.
And from MDN Webdocs:
the result attribute contains the raw binary data from the file.
Note that this method was once removed from the File API specification, but re-introduced for backward compatibility.
Using FileReader.readAsArrayBuffer() is recommended.
Sources:
https://stackoverflow.com/questions/36500196/difference-between-readasdataurl-and-readasarraybuffer-and-readastext-in-j
https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsBinaryString
5
0