Building in Public - 2
Bryan Liao
Posted on June 11, 2024
I haven’t worked with file uploads from scratch before, so I did some exploration.
Adding a component for file uploads is actually quite simple, it’s adding an <input>
tag with a type="file"
attribute. Through normal browser code, you can access this using document.getElemenyById
and add an event listener to work with the upload. Since I’m using React I used the onChange
attribute and a state hook.
Something that caught me off guard was the callback. I usually use e.target.value
, but I learned that this will just be a filepath as a String, not the actual file itself. Instead, I had to use the files
property (i.e e.target.files
) to access the FileList from the input element and could read the file contents from that.
The more you know 🌟
Posted on June 11, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
September 29, 2024