Skip to content

Instantly share code, notes, and snippets.

@dongyuwei
Created June 14, 2016 03:21
Show Gist options
  • Save dongyuwei/9852638f38bda9b1c2a7e1545efdd657 to your computer and use it in GitHub Desktop.
Save dongyuwei/9852638f38bda9b1c2a7e1545efdd657 to your computer and use it in GitHub Desktop.
use jschardet in browser
// load jschardet js lib
// $.getScript('https://cdnjs.cloudflare.com/ajax/libs/jschardet/1.4.1/jschardet.min.js')
var xhr = new XMLHttpRequest(), blob;
xhr.open("GET", "http://127.0.0.1:5000/api/uploads/404/blob", true);
// Set the responseType to blob
xhr.responseType = "blob";
xhr.addEventListener("load", function() {
if (xhr.status === 200) {
console.log("Image retrieved");
var blob = xhr.response;
var reader = new FileReader();
reader.addEventListener("loadend", function() {
console.log(jschardet.detect(reader.result))
});
reader.readAsBinaryString(blob);
}
}, false);
xhr.send();
@dongyuwei
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment