admin管理员组文章数量:1197185
So as an example, when I read the π character (\u03C0
) from a File using the FileReader API, I get the pi character back to me when I read it using FileReader.readAsText(blob)
which is expected. But when I use FileReader.readAsBinaryString(blob)
, I get the result \xcf\x80
instead, which doesn't seem to have any visible correlation with the pi character. What's going on? (This probably has something to do with the way UTF-8/16 is encoded...)
So as an example, when I read the π character (\u03C0
) from a File using the FileReader API, I get the pi character back to me when I read it using FileReader.readAsText(blob)
which is expected. But when I use FileReader.readAsBinaryString(blob)
, I get the result \xcf\x80
instead, which doesn't seem to have any visible correlation with the pi character. What's going on? (This probably has something to do with the way UTF-8/16 is encoded...)
2 Answers
Reset to default 22FileReader.readAsText
takes the encoding of the file into account. In particular, since you have the file encoded in UTF-8, there may be multiple bytes per character. Reading it as text, the UTF-8 is read as it is, and you get your string.
FileReader.readAsBinaryString
, on the other hand, does exactly what it says. It reads the file byte by byte. It doesn't recognise multi-byte characters, which in particular is good news for binary files (basically anything except a text file). Since π is a two-byte character, you get the two individual bytes that make it up in your string.
This difference can be seen in many places. In particular when encoding is lost and you see characters like é displayed as é.
Oh well, if that's all you needed... :)
CF80
is the UTF-8 encoding for π.
本文标签: javascriptDifference between readAsBinaryString and readAsText using FileReaderStack Overflow
版权声明:本文标题:javascript - Difference between readAsBinaryString and readAsText using FileReader - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738551084a2097384.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
CF80
is the UTF-8 encoding for π. – deceze ♦ Commented Feb 19, 2012 at 2:15