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)=>{constfiles=event.target.files;constfile=files[0];reader.readAsDataURL(file);reader.addEventListener('progress',updateProgressBar);reader.addEventListener('loadend',updateProgressBar);functionupdateProgressBar(event){if(!event.loaded||!event.total){return;}constpercent=(event.loaded/event.total)*100;progress.value=percent;document.getElementById('progress-label').innerHTML=Math.round(percent)+'%';if(percent===100){letmsg=`<span style="color:green;">File <u><b>${file.name}</b></u> has been uploaded successfully.</span>`;feedback.innerHTML=msg;}}});
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
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.
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&default-tab=result&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&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
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?
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.
I am a Full stack .NET Developer, I like to work with C#, .Net Core, SQL, Mongo DB, Azure, JavaScript , Angular...
Always eager to learn new technologies. I am here to share, ask & eventually learn.
I am a software developer helping car dealership businesses convert leads into actual customers. When I am not helping businesses grow, I learn new things, and share them for others to learn from.
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?
We're a place where coders share, stay up-to-date and grow their careers.
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
progressevent was fired around the 1% mark for a couple of files, but wasn't fired again beforereadyStateindicated that the file upload was done. For a production implementation, you'll probably also want to hook into theloadendevent to account for this. Here's a slightly modified version of the above example that demonstrates this:Great point Payton! I am gonna look into that and modify. Thanks for reading and commenting.
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.ioHey 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.
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&default-tab=result&embed-version=2. But I didn't notice last time that there was a sub-iframe that CodePen put in with src valuehttps://cdpn.io/atapas/fullembedgrid/BazzaoN?animations=run&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
Sure, thanks Daniel.
Learned a lot! Thanks
Thank you Natnael. Very glad as it was useful to you!
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?
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!
Very useful post, 😄
Glad it was useful, Shaijut.
Heh, file upload has so many weird quirks, especially when dealing with images, i just use Uppy and go on with more important tasks ;)
Great artical, Thanks so much :)
Thank you for reading and liking!
Thank you very much
Good article. Thank you 🙂👍
Thank you, Brian!
Wonderful article!
Thank you very much!
Think this is the best HTML article ive read.
Yaay... That's huge! Thank you very much.
Great, thanks for sharing!
Thank you!
Thank you so much for the explanation.
Thank you very much.. Glad it was useful!
good one 👍🏻✨
Thanks so much for this post. I now have a much better understanding of the file upload system.
Wow, Thanks Neelam! Glad it was useful.
Thanks a lot for this. Best article I've read today
Thank you Mateen!
Thankyou so much! 😇
Thank you!
Thanks Jordan.
Thank you for such an amazing tips.
Thanks Maxwell.. Glad it was helpful.
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?