filereader.js icon indicating copy to clipboard operation
filereader.js copied to clipboard

Text file encoding support (UTF8 / ANSI)

Open azev opened this issue 9 years ago • 5 comments

Superb plugin! Please, if possible, add text file encodind option for reading the file contents.

azev avatar Apr 05 '15 21:04 azev

OK so you would need a way to hook into the arguments passed into readAsText 0. Do you want to switch the encoding per-file or globally? This option could be added as a global configuration value, or could be attached onto the file object in the onbeforestart callback.

bgrins avatar Apr 14 '15 20:04 bgrins

It would be nice to have a feature to "autodiscover" the encoding of text files (ANSI/UTF-8). But it could be a parameter as you suggested.

var FileReaderOptions = { readAsDefault: "Text", encoding: "ANSI", // or "UTF-8" on: { load: function(e, file) { loadPlainText(e.target.result); } } } FileReaderJS.setupDrop($('#dropper').get(0), FileReaderOptions);

Thanks

azev avatar Apr 14 '15 21:04 azev

Seems good, with the caveat that the default for encoding should be undefined (so that we keep spec behavior for people who don't specify an option). I'm not going to be able to implement this at the moment, but I'd be happy to accept a PR.

Regarding auto discovery, this would be even better but I don't know of any way to do that with the current API - the encoding must be specified as the second parameter to readAsText

bgrins avatar Apr 14 '15 22:04 bgrins

I tried this: http://jsfromhell.com/geral/utf-8

It didn't work for me.

I'd like to contribute. However I'm confused how github works. I don't know how to contribute.

Is there a way to switch between ANSI/UTF-8 as suggested here? https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsText

azev avatar Apr 14 '15 22:04 azev

I'd like to contribute. However I'm confused how github works. I don't know how to contribute.

You can fork the repository and then make the changes locally and create a pull request.

Is there a way to switch between ANSI/UTF-8 as suggested here?

Around this line you could add something like:

var readAsOptions = undefined;
if (readAs === "readAsText") {
    readAsOptions = whatever encoding;
}

reader[readAs](file, readAsOptions);

bgrins avatar Apr 15 '15 02:04 bgrins