Convert gbk text to UTF8 encoding (chrome, firefox only) by using javascript in the browser, and utf8chrome
When we use xmlhttprequest to retrieve text content, if the server returns gbk (or gb2312 or other non-utf8 encoded content), a bunch of garbled characters are obtained, how do I convert to the default UTF-8 encoded text in the browser?
In fact, if you are using the chrome or firefox browser, it is very simple. You can use the built-in browser object TextDecoder for conversion.
Javascript sample code:
if ('TextDecoder' in window) { var files = { 'gbk.txt': 'gbk' }; Object.keys(files).forEach(function(file) { fetchAndDecode(file, files[file]); }); } else { console.error('Your browser does not support the Encoding API.'); } function fetchAndDecode(file, encoding) { var xhr = new XMLHttpRequest(); xhr.open('GET', file); xhr.responseType = 'arraybuffer'; xhr.onload = function() { if (this.status == 200) { var dataView = new DataView(this.response); var decoder = new TextDecoder(encoding); var decodedString = decoder.decode(dataView); console.info(decodedString); } else { console.error('Error while requesting', file, this); } }; xhr.send(); }
In fact, not only gbk, as long as the encoding supported by the browser (see link 3), can be easily converted.
Refer:
1. https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
2. Easier ArrayBuffer String conversion with the Encoding API
3. https://encoding.spec.whatwg.org/