DEV Community

Cover image for 10 useful HTML file upload tips for web developers

10 useful HTML file upload tips for web developers

Tapas Adhikary on October 20, 2020

Introduction The ability to upload files is a key requirement for many web and mobile applications. From uploading your photo on social ...
Collapse
 
p810 profile image
Payton Bice • Edited

Something to note about the progress bar example is that it may not finish for users who have fast connections. I was testing it out and noticed that a progress event was fired around the 1% mark for a couple of files, but wasn't fired again before readyState indicated that the file upload was done. For a production implementation, you'll probably also want to hook into the loadend event to account for this. Here's a slightly modified version of the above example that demonstrates this:

fileUploader.addEventListener('change', (event) => {
  const files = event.target.files;
  const file = files[0];
  reader.readAsDataURL(file);

  reader.addEventListener('progress', updateProgressBar);
  reader.addEventListener('loadend', updateProgressBar);

  function updateProgressBar(event) {
    if (! event.loaded || ! event.total) {
      return;
    }

    const percent = (event.loaded / event.total) * 100;
    progress.value = percent;
    document.getElementById('progress-label').innerHTML = Math.round(percent) + '%';

    if (percent === 100) {
      let msg = `<span style="color:green;">File <u><b>${file.name}</b></u> has been uploaded successfully.</span>`;
      feedback.innerHTML = msg;
    }
  }
});
Enter fullscreen mode Exit fullscreen mode
Collapse
 
atapas profile image
Tapas Adhikary

Great point Payton! I am gonna look into that and modify. Thanks for reading and commenting.

Collapse
 
danielfinke profile image
Daniel Finke • Edited

What specifies the domain of the Object URLs? In the embedded CodePen example #10, the URL ends up something like blob:https://cdpn.io/5a61e13d-0c89-490c-8cce-d6bf06483b9b. Not understanding how it came up with cdpn.io

Collapse
 
atapas profile image
Tapas Adhikary

Hey Daniel, thanks for mentioning that. The object url doesn't mention any domain for this example. It is just expected to get the image converted for this example.

I have rechecked and found it working when I upload an image. Could you please elaborate the steps to get the problem mentioned? I am interested to know that and check.

Collapse
 
danielfinke profile image
Daniel Finke

Hi Tapas, I was just examining the src attribute of the image in the iframe after the image was uploaded.

I figured out why I was confused though. In your post, the CodePen iframe has the src value https://codepen.io/atapas/embed/BazzaoN?height=600&amp;default-tab=result&amp;embed-version=2. But I didn't notice last time that there was a sub-iframe that CodePen put in with src value https://cdpn.io/atapas/fullembedgrid/BazzaoN?animations=run&amp;type=embed. So it seems like the blob URL is just based on the URL of the page or frame that the JS is running in.

Seems kind of weird that the browser adds the domain in when the blob is local to you machine only anyway

Thread Thread
 
atapas profile image
Tapas Adhikary

Sure, thanks Daniel.

Collapse
 
johnhorner profile image
John Horner

This is very interesting but a true upload would write the file out to a server, with all the complexities that involves. File name rules, permissions, paths, umasks etc. Using this code what have we actually uploaded to? The browser’s memory?

Collapse
 
atapas profile image
Tapas Adhikary

Yes John, you are right.

The scope of the article was to introduce the basics and focusing on the Html file upload tag and client side file object.

I am writing a series now to build on top of it to showcase with server side pieces using Node.js. Hoping that will get you what you are asking for. Please stay tuned.

Thanks for reading and commenting!

Collapse
 
yagizhan14 profile image
Yagizhan Avci

Thank you so much for the explanation.

Collapse
 
atapas profile image
Tapas Adhikary

Thank you very much.. Glad it was useful!

Collapse
 
olsard profile image
olsard

Great, thanks for sharing!

Collapse
 
atapas profile image
Tapas Adhikary

Thank you!

Collapse
 
nicklleite profile image
Nicholas @ 127.0.0.1 (::1)

Think this is the best HTML article ive read.

Collapse
 
atapas profile image
Tapas Adhikary

Yaay... That's huge! Thank you very much.

Collapse
 
chidioguejiofor profile image
Chidiebere Ogujeiofor

Wonderful article!

Collapse
 
atapas profile image
Tapas Adhikary

Thank you very much!

Collapse
 
mateenui profile image
Mateen Gbadamosi

Thanks a lot for this. Best article I've read today

Collapse
 
atapas profile image
Tapas Adhikary

Thank you Mateen!

Collapse
 
zeshama profile image
Natnael Getenew

Learned a lot! Thanks

Collapse
 
atapas profile image
Tapas Adhikary

Thank you Natnael. Very glad as it was useful to you!

Collapse
 
brianmanden profile image
Brian Juul Andersen

Good article. Thank you 🙂👍

Collapse
 
atapas profile image
Tapas Adhikary

Thank you, Brian!

Collapse
 
artydev profile image
artydev

Thank you very much

Collapse
 
n0sys73m1ss4f3 profile image
z3r0

Great artical, Thanks so much :)

Collapse
 
atapas profile image
Tapas Adhikary

Thank you for reading and liking!

Collapse
 
shaijut profile image
Shaiju T

Very useful post, 😄

Collapse
 
atapas profile image
Tapas Adhikary

Glad it was useful, Shaijut.

Collapse
 
pavelloz profile image
Paweł Kowalski

Heh, file upload has so many weird quirks, especially when dealing with images, i just use Uppy and go on with more important tasks ;)

Collapse
 
neelam28 profile image
Neelam

Thanks so much for this post. I now have a much better understanding of the file upload system.

Collapse
 
atapas profile image
Tapas Adhikary

Wow, Thanks Neelam! Glad it was useful.

Collapse
 
onurcelik profile image
Mustafa Onur Çelik

good one 👍🏻✨

Collapse
 
kritikaramesh profile image
kritikaramesh

Thankyou so much! 😇

Collapse
 
atapas profile image
Tapas Adhikary

Thank you!

Collapse
 
ghkobbs profile image
Maxwell Morrison

Thank you for such an amazing tips.

Collapse
 
atapas profile image
Tapas Adhikary

Thanks Maxwell.. Glad it was helpful.

Collapse
 
kansoldev profile image
Yahaya Oyinkansola

This was quite an insightful read of HTML file upload functionality, even in 2024, this is very valuable! Has anything changed about this over the years?

Collapse
 
atapas profile image
Tapas Adhikary

Thanks Jordan.