Hey, I’m using the last version (0.19.1) of Elm and I’ve got an issue with how file upload is coded.
I’m using the elm function File.toUrl but this got me a console error (reader.addEventListener is not a function).
So I checked my js compiled file to see where is this from, and this is the js function :
function _File_toUrl(blob) {
return _Scheduler_binding(function (callback) {
var reader = new FileReader();
reader.addEventListener('loadend', function () {
callback(_Scheduler_succeed(reader.result));
});
reader.readAsDataURL(blob);
return function () { reader.abort(); };
});
}
So this piece of code if producing the error.
If I change it to :
function _File_toUrl(blob) {
return _Scheduler_binding(function (callback) {
var reader = new FileReader();
reader.onloadend = function () {
callback(_Scheduler_succeed(reader.result));
};
reader.readAsDataURL(blob);
return function () { reader.abort(); };
});
}
Then it works as expected.
From what I understant, FileReader is not an element so we cannot attach an event listener to it.
I have just tried using File.toUrl in my app and it worked just fine. I’m using Firefox on Ubuntu 22.04. I have successfully used addEventListener on a new FileReader() and this page from MDN seems to indicate this is normal: FileReader: load event - Web APIs | MDN
Could you maybe give more details about your compilation/build process? I know (because I experienced it myself) that sometimes elm-optimize-level-2 can break stuff. Or maybe you have some third-party code messing with it (browser extension or other JS that may mess with the FileReader prototype)?
Good luck!
PS: did you work with Spottt 10 years ago or was it another software developer with the same name?
I think your lines are incorrect, in that it’s not on the class you should be calling the functions (which the code doesn’t do originally).
So you need to try: var reader = new FileReader(); reader.addEventListener('load', function(){});
and var reader = new FileReader(); reader.onload = function(){};
I have tried in both Brave and Chrome (Version 127.0.6533.119 (Official Build) (64-bit)) on Ubuntu 22.04 and they seem to work alright. I don’t have a Mac to test them on.
Interestingly, when using some optimization/uglyfying script, new FileReader() gets rewritten to new FileReader. I don’t know how significant it is.