bgrins / filereader.js

A lightweight wrapper for the JavaScript FileReader interface
http://bgrins.github.com/filereader.js/
MIT License
413 stars 86 forks source link

Text file encoding support (UTF8 / ANSI) #20

Open azev opened 9 years ago

azev commented 9 years ago

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

bgrins commented 9 years ago

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.

azev commented 9 years ago

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

bgrins commented 9 years ago

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

azev commented 9 years ago

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

bgrins commented 9 years ago

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);