File upload is a common feature in many web applications that was originally done using web forms with multipart/form-data
The example below shows a file picker that returns the size of uploaded files.
First, an onchange
event listener is attached to the file picker:
Then, the file is obtained from the event object the listener provides – the first file in the files array is picked because we are dealing with single files.
Next, an Ajax call is made to the endpoint that expects a file upload. In this case, the file is: https://express-file-server.vicradon.repl.co/upload
.
Further logic, like notifying, the user can be done in the .then
block. If any HTTP errors are encountered, they are handled in the .catch
block.