0

I've seen a few questions relating to this, but almost all of them use php. I have just a simple html file, with an input and a button. I've tried using proposed solutions, but I always get the error after I submit the form with the button. I have included the character enconding as utf-8. I also have the <meta charset="utf-8" /> but commented out upon seeing other solutions on SO, which I have implemented to no avail.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <script src="index.js"></script>
    <title>My Page</title>
</head>
<body>
    <form name="data-form" enctype="multipart/form-data" method="post">
        <input type="file" id="file-input">
        <button id="form-btn" type="submit">Load File</button>
    </form>
</body>
</html>

Based on my rudimentary understanding, this should read the file no problem. I'm trying to upload a JSON file, not sure if that's the issue. It is a valid JSON, I have checked and double-checked.

The index.js file that is linked, currently does not interact with the file upload in anyway, so in the interest of brevity I have not included it.

EDIT

I "solved" this, I guess. More like bypassed it. I wrote a JS function (readFile()) to read on click, and changed the button's type to type="button" and put an onclick="readFile()" and now it reads the file and eecutes properly. Hopefully maybe this will help someone else who has a similar problem. I guess this can be closed.

nos codemos
  • 569
  • 1
  • 5
  • 19
  • Where do you get the error? Browser console? Form handler? Do you need an `action` attribute on the form or do you send it via JavaScript? – Peter Krebs Nov 10 '21 at 09:59
  • 1
    Yes, it shows up in the console. I don't send it with JS (at least not yet) so maybe I do need an `action` attr. I will read the MDN docs and see if that leads to enlightenment. – nos codemos Nov 10 '21 at 10:09
  • If you don't send the form via JS how can that error show up in the browser console? The network tab can also be very helpful. – Peter Krebs Nov 10 '21 at 10:14
  • I have no idea, if I did I probably wouldn't be here asking the question. The javascript file does nothing at the moment because none of the functions are "plugged in" to the `index.html`. If I change the button `type="button"` I don't get an error, but also nothing happens. I'm not sure if that's better or worse. – nos codemos Nov 10 '21 at 10:17
  • Well I'm just asking to make you understand it yourself. Seems you solved your problem. – Peter Krebs Nov 10 '21 at 10:40

1 Answers1

0

See edit. I used a modified version of the IFFE answer posted in this question How to access data of uploaded JSON file?. I'm not sure if this is "technically" the solution but it works for my purposes.

nos codemos
  • 569
  • 1
  • 5
  • 19