Warning – the demos are for this tutorial went a little crazy when I recently made a change to the jquery file. I will be working to bring the demos for this tutorial back online. Until then, don’t view the tutorials if you are using IE.
Many months ago, when trying to catch up to all the AJAX hype, I was surfing the FiftyFourEleven site looking at wonderful examples of innovative javascript code when I stumbled up some code for “Upload Multiple Files With A Single File Element“.
So when I decided to launch 15 Days of jQuery, this was one of the very first scripts that I wanted to give a jQuery makeover.
Looking through my server logs a few days ago I see that I’ve got a trackback from a site I don’t recognize. I check it out only to discover that two of my jQuery tutorials are being listed as an example of what the author hates about javascript.
According to this twerp, any tool or technique that doesn’t put accessibility as the numero uno priority is bad.
Although I strongly disagree with this widely held belief, it got me thinking about this particular tutorial. I went back to the drawing board and created a way to create a similar effect unobtrusively… so that if my critic decides to visit with javascript turned off, he’ll be able to use the form too.
Goal #1: To permit multiple file uploads using one file input element… and to make the whole interaction sexy.
Goal #2: To make multiple file uploads sexy… without sacrificing usability. The focus here is unobtrusive javascript to shape a form with multiple file input fields.
Demonstration one – only one file input element, but with the addition of jQuery and some custom code that you’re about to see it becomes a user-friendly multiple file upload script.
Multiple file upload with one file input
Demonstration two – multiple file input elements in the (x)html source of the form but jQuery modifies what the visitor sees and gives a similar feel to the first demonstration. The advantage to this method is that it is unobtrusive… with javascript turned off the visitor can still upload multiple files.
Multiple file upload with several file inputs
Single file input –
The jQuery $(document).ready() function does two things:
Creates a div where the maximum files allowed is shown to the visitor.
Finds the file upload field (assuming there is only one) and attaches an onChange event to it.
$("input[@type=file]").change(function(){
doIt(this, fileMax);
});
The doIt() function (nice name, huh?) checks to see if the maximum file limit has been reached, and if not, it hides the file input field, adds a new one inside the containing div, puts the name of the file chosen inside the div with id “files_list”, and adds a Delete button on the end.
To navigate the DOM tree I use jQuery’s parent() function and then remove elements using the remove() function. I also make use of append() and prepend() to add in the file names and new input fields respectively.
Two key points:
1- You determine the maximum number of files allowed with this line
var fileMax = 3;
2- The file input fields must be named appropriately
<input type="file" class="upload" name="fileX[]" />
I do this so that the fields can be added and removed by the visitor without any concern for keeping track of id’s or names. When the form is submitted to a server side script, the information is sent in an array that can be easily traversed.
Multiple file input –
This was was trickier to pull off.
First, the number of files allowed is determined by the number of file input fields in your (x)html. Second, you should still name them in a way that stacks the field information into an array.
<input type="file" class="upload" name="fileX[]" />
The big difference in this second version is that I loop through each file input field and apply the doIt() function when the field value changes. By looping through each one, I can send an additional piece of information that’s critical to my code: the order of the field in the “stack”.
In other words, as the code executes, it’s specifically targeting the first input field, or the second, or the third.
The code for this is found here:
$("input[@type=file]:nth-of-type("+n+")")
jQuery’s flexibility allows me to use CSS and XPath descriptions to target specific elements.
You’ll notice that as each file is chosen, the file input field is replaced by the name of the file. Clicking on the name of the file allows you to choose a different file.
[tags]multiple file upload, javascript, DOM, unobtrusive, jQuery[/tags]
90 Responses
Antonio
June 20th, 2006 at 6:49 am
1Perfect. Right when I needed to upload files you publish this. I’m happy
Typo: “mutliple” in “Technorati tags”
Jack
June 20th, 2006 at 9:10 am
2Thanks for picking up on that typo… it’s fixed now.
fartikus
June 21st, 2006 at 12:27 am
3not sure if i would refer to an accessibility zealot as a “twerp”…and turning off javascript is not just an accessibility issue. i run the noscript extension. everyone should. who doesn’t?? its not that i refuse to allow js to execute, but i won’t allow third-party domain code, and i won’t allow code i don’t see an appropriate benefit in.
fartikus
June 21st, 2006 at 12:34 am
4by the way, the xpath selector – very powerful. do other toolkits support this?
jquery strikes me as the perl of js toolkits – while the syntax may appear noisy to some, it in fact has incredible economy and some nice idioms
Jack
June 21st, 2006 at 11:17 am
5Fartikus,
Just to be clear, I was not saying that anyone interested and enthusiastic about accessibility is a twerp. But this particular blogger is – in my opinion.
The tutorials he was bashing clearly point out the accessibility issues and advise the reader to decide for himself if the benefits of the prescribed technique outweigh the downside.
Besides, one of the main goals of this blog is to introduce the world to jQuery, show some of the unique features, and how it can be applied.
tripdragon
June 22nd, 2006 at 8:52 am
6it’s cute but soooo old… Can you post and example of a drag to box so we can just drag a bunch of files to the window and have those upload ??
Far faster and less fiddly
Jack
June 22nd, 2006 at 9:06 am
7tripdragon,
Glad you came by even though my tutorial didn’t meet your expectations.
First, do you have a link to an example that is currently written using a different javascript library? Or are you proposing something you haven’t seen before.
Just thinking out loud, I think you’d have to open up the age-old “browse…” button and then go navigate to find the file on your hard drive.
A quick Google search seems to support this. I see mention of JAVA applets and situations for users with Firefox that make specific changes to their browser options.
Matthijs
July 8th, 2006 at 4:18 am
8“it’s cute but soo old”
Drag and drop in this situation would not be better, in my opinion.
Say you’re on a website on which you can upload your pictures. You’d have to resize your browser to make room for the windows(?) explorer, which you have to open also if it isn’t already. Then in the explorer you’d have to browse to the directory and files you’d want uploaded. Then you’d have to resize both windows so it is possible to drag and drop. Then you’d have to perform the actual drag and drop. How many steps are this?
I’m not even talking about people who wouldn’t understand the whole process, don’t want or cannot use a mouse, etc
Clicking “browse” and getting a popup to go to the file you’d want to upload is a common way which most people are familiar with nowadays.
GalneP
September 17th, 2006 at 7:39 pm
9Very cool widget. Thank you for your great tutorials.
One note. As far as I can tell the demo doesn’t seem to work with the latest jQuery release:
* $Date: 2006-08-31 13:26:31 -0400 (Thu, 31 Aug 2006) $
* $Rev: 249 $
As far as I can tell (and I’m pretty new to JavaScript not to mention jQuery) the following line…
var fileName = $(“input[@type=file]:nth-of-type(“+n+”)”).val();
is assigning “null” to fileName. Anyway, I can’t complain. In fact I’d love to see “another 15 days of jQuery”.
Jack
September 18th, 2006 at 4:36 pm
10Galne,
Thanks for the compliments.
Demo1 does work with the latest jquery. I just uploaded the latest (same one you refer to) and it works fine. The second one breaks.
But no, the line you reference is not setting the value to null. In the jquery documentation is specifies that val() grabs the value, val(‘foo’) sets the value to foo.
austin
September 18th, 2006 at 9:44 pm
11In the 1st demo, why is it that when i delete the (queued) files, most of the times, the counter does not revert or deduct from the current file count, thus, prompting me that I have max’ed out the no. of permitted files (for upload) when in fact, i have deleted them. Also, I’m having some problems with IE6.
Pretty sleek tutorials though!
Jack
September 18th, 2006 at 10:24 pm
12Austin,
After some testing, I was able to reproduce it under a specific situation: if you maxed out the number of files, got the error message, and then went to delete and then re-add files, you got a an error, when in fact you shouldn’t. (confused yet?)
I was missing a return true;
function doIt(obj, fm) { if($('input.upload').size() > fm) {alert('Max files is '+fm); obj.value='';return true;}And darn it… I see what you mean about IE. Crap. I’ll have to take this tutorial down until I figure out what the deal is. I updated the jquery.js file to the latest and things went screwy.
George
October 5th, 2006 at 6:34 am
13The fault with the demo may be linked to the use of the :nth-of-type() selector that is not in the recent jquery releases. You may be able to use :nth-child() instead (though it won’t distinguish between input types), or use :nth-of-type() available in the moreSelectors jQuery plugin at http://www.softwareunity.com/sandbox/JQueryMoreSelectors. (Just a suggestion. I’ve not studied your code)
Jack
October 6th, 2006 at 1:16 am
14George,
You’re the bomb!
I don’t know why I didn’t see the reson why before. Apparently nth-of-type didn’t make the cut when it was recently decided what was core and what wasn’t.
Here’s one of the fixed demos.
http://15daysofjquery.com/examples/jqueryMultiFile/demo4.php
(Crossing that one off the list!)
Thanks again.
Philip
October 30th, 2006 at 1:34 am
15I tried the demo (firefox 1.507) and uploaded 3 dummy files. Then clicked on reset. It created an extra reset button, up to at least six of them.
Next question: is there a way to add an upload progress meter to tell me how much or how many files have been uploaded during the process?
Wonderful work!
Jack
October 30th, 2006 at 11:36 am
16Progress demo generally can’t be done using PHP. I’ve researched it pretty extensively, and although I don’t have a link handy, if you Google it you’ll see that there are very few workarounds, most of them involving a java applet.
Vince
December 17th, 2006 at 7:46 pm
17uhmm i know you disabled it from actually uploading the files.. i think.. i’m kind of a noob but this is a great script. The best file uploader i have found and… would like to know how to finish it so it uploads the files?
Jack
December 19th, 2006 at 10:39 pm
18Vince,
Yes, the upload capability is disabled in the demo.
I have a contact form processor script that does a great job handling file uploads.
http://www.ultimateformmail.com/jquery-special.php
Nate
February 7th, 2007 at 12:07 pm
19Any estimate on when the second demo might be working again? Also, might it be possible to see a demo that loads a thumbnail of each image as they are uploaded? Many thanks for what you have done already. This looks very exciting.
Jack
February 7th, 2007 at 2:44 pm
20Right around the corner, in a nice, neat plugin. Thumbnails… hmmm, not as a demo, sorry.
Nate
February 7th, 2007 at 3:38 pm
21Would the thumbnail feature be difficult to code? Any hints or ideas on how to do that? Has it been done before? I’ve searched the web for an example or demo script that can do this, but I’ve not found any so far. There was one javascript that worked in IE 6 only, but who uses IE 6?
Jack
February 7th, 2007 at 3:50 pm
22Thumbnail can’t be generated until the file is uploaded to the server. If you are uploading three files, they get sent at the same time, under normal conditions.
The thumbnail generation is PHP, server side. Javascript can’t do this alone.
Sorry.
Nate
February 8th, 2007 at 3:29 pm
23Right, you need to use both a server side script along with Javascript together to do this. I have a function in Coldfusion that will create the thumbnail. The part that I can’t figure out is how to use jquery to submit the file to my Coldfusion script and then create the img src html to show the thumbnail without refreshing the page. I imagine you could use an iframe and some jquery code to do this, but I’m not sure how. If anyone has tip or suggestion for me, I’d love to hear it.
Oli
March 8th, 2007 at 2:01 pm
24the IE-Problem drove me crazy!
the solution for demo1.php …
…find(“input:visible”).change(function(){ doIt(this);
});
(bind the new change-event on the visible input)
Nate
March 26th, 2007 at 7:09 pm
25Has anyone seen the swfupload: http://swfupload.mammon.se/
This is awesome because you can shift click to upload many fils at once. I’d love to see this one redone with jquery instead of Javascript. Anyone want to do that and post the jquery code for the rest of us to use?
Jack
March 27th, 2007 at 10:41 am
26Nate,
I haven’t seen that one. I have a plugin that comes close, but doesn’t do the shift click. It opens a normal browse window. I suspect that Flash is what gives that other code the extra functionality.
By the way, jQuery is a javascript library.
Nate
March 27th, 2007 at 11:25 am
27Flash is indeed what is used there. I’d love to see an example that didn’t require Flash for the shift click feature to work, but I’m not sure it exists in the open source world yet.
Diego
April 11th, 2007 at 5:23 am
28Last week I released a plugin very similar to this. People interested on this post may also find the following useful:
http://www.fyneworks.com/jquery/multiple-file-upload/
Jack
April 11th, 2007 at 9:22 am
29Diego,
Very cool. You beat me to it. Good job.
Mike
June 13th, 2007 at 2:59 pm
30I am having a big issue making anything with multifile work with safari.. does anyone see this problem and know of a solution?
vid
June 21st, 2007 at 9:19 am
31good scripts i going to give it a try,
then will test for safari too ( we must make it work on safari becourse of it’s popular now come to window os )
will get back asap
vjeran
August 29th, 2007 at 6:35 am
32Hmm.. well nice. i have made multiple files upload in flash. Now i am looking to jquery and YUI ext. Jquerry looks very simple and nice and i would add CSS framework. BUt still i don’t know what to choose.
Snowman53
April 18th, 2008 at 9:52 pm
33This topic has been posted on the Thickbox forum, but no workable solution has been offered, or at least I couldn’t recognize one if it was.
Hopefully someone can walk me through a solution!
Here is an example of a TB call I am making (using: jquery-1.2.3, TB 3.1, IE7);
Edit
TB opens with the default size and located near the top of the page instead of using my sizes and centering on the screen when the page is scrolled down.
If I use the same code without passing a variable to the destination URL, TB works as expected (fantastically).
So clearly passing the URL variable messes up passing variables to TB. Could someone walk me through how to change this so it works correctly?
Note that the URL variable is generated by the ASP code and is not easily modified, so I need a Thickbox oriented solution (if possible).
Thanks!
manh ha
April 23rd, 2008 at 5:55 am
34I found some bugs, if play it in ie6 when choose one file, it auto add o max.
To fix this problem REPLACE:
$(obj).parent().prepend(”).find(“input”).change(function() {doIt(this, fm)});
BY
$(obj).parent().prepend(”);
manh ha
April 23rd, 2008 at 5:57 am
35$(obj).parent().prepend(”);
manh ha
April 23rd, 2008 at 5:57 am
36oopts fix html text
manh ha
April 23rd, 2008 at 5:59 am
37$(obj).parent().prepend(‘input type=”file” class=”upload” name=”fileX[]” onchange=”doIt(this, ‘+fm+’);”‘);
Marakas
May 20th, 2008 at 2:58 am
38@Oli -> Thanks for providing the ie fix, that was driving me mad as well.
scripter
June 20th, 2008 at 5:10 am
39@Oli too – thanks for the ie fix
jay
August 7th, 2008 at 5:24 am
40Dear Sir
Thanks for code.
Only one bug that not work multiple upload in IE7.
With Regards Jay Bharat
http://www.bharatbaba.com
25 Awesome tutorials for web designers « Narendra Dhami
August 11th, 2008 at 9:01 pm
41[...] 17. Multiple File Upload Magic With Unobtrusive JavascriptThis tutorial illustrates how to upload multiple file with one ore several file inputs using jQuery. [...]
25 Awesome tutorials for web designers « Guiwells’s Blog
August 12th, 2008 at 1:32 am
42[...] 17. Multiple File Upload Magic With Unobtrusive JavascriptThis tutorial illustrates how to upload multiple file with one ore several file inputs using jQuery. [...]
From free for free » Blog Archive » 25 Awesome tutorials for web designers
August 12th, 2008 at 5:25 am
43[...] 17. Multiple File Upload Magic With Unobtrusive JavascriptThis tutorial illustrates how to upload multiple file with one ore several file inputs using jQuery. [...]
25 Awesome tutorials for web designers | Boxed CSS
August 16th, 2008 at 10:54 am
44[...] 17. Multiple File Upload Magic With Unobtrusive JavascriptThis tutorial illustrates how to upload multiple file with one ore several file inputs using jQuery. [...]
Caker on Web » Blog Archive » jQuery links
August 22nd, 2008 at 5:17 am
45[...] Multiple File Upload Magic With Unobtrusive Javascript [...]
25 Awesome tutorials for web designers | 打篮çƒçš„æ‰‹
September 8th, 2008 at 6:27 am
46[...] 17. Multiple File Upload Magic With Unobtrusive Javascript This tutorial illustrates how to upload multiple file with one ore several file inputs using jQuery. [...]
RSSAèšåˆ » 25 Awesome tutorials for web designers
September 9th, 2008 at 5:44 am
47[...] 17. Multiple File Upload Magic With Unobtrusive Javascript This tutorial illustrates how to upload multiple file with one ore several file inputs using jQuery. [...]
omer sheriff.h
October 14th, 2008 at 4:51 am
48one file is upload then another
file is update time previous file
content cleared
Webagentur
December 12th, 2008 at 1:14 pm
49Thank you … this tutorial has me very helped.
Paul
January 21st, 2009 at 1:14 pm
50Yet another good tutorial! I wish i had the time!
M.A.Yoosuf
January 30th, 2009 at 12:50 pm
51total bug in IE, please help me, the Client is making a Big puss because of the bug
Basically when selecting the file, it repeats for many times
40+ Excellent jQuery Tutorials | instantShift
February 9th, 2009 at 3:06 pm
52[...] Tutorial Link: Click Here [...]
40?????jQuery???? - Code Index
February 23rd, 2009 at 4:05 am
53[...] Tutorial Link: Click Here [...]
??jQuery????? | jquery ajax??????
April 3rd, 2009 at 9:49 am
54[...] Multiple File Upload Magic With Unobtrusive Javascript [...]
Jake Scott
August 27th, 2009 at 6:09 pm
55Hey dude you should update this post to the latest version of jQuery because your Selectors for attributes are out of date!
$(‘input[@type=file]‘)
Shoult be
$(‘input[type="file"]‘)
praveen
September 2nd, 2009 at 5:03 am
56Dear Sir
I Want to make a file upload form when i click submit it simply uploads to an web address , please tell how to make this
From
R.Praveen Kumar
78 jQuery Scenarios to Fall in Love | ProgrammerFish - Everything that's programmed!
October 7th, 2009 at 11:14 pm
57[...] 76)Multiple file upload magic with unobtrusive java script [...]
shafiahamed
October 12th, 2009 at 1:01 am
58Hello,
I would like say thanks to you i will find code to add multiple images its to short but its cool
40+ Excellent jQuery Tutorials « PSD to HTML , Slicing PSD to HTML
January 10th, 2010 at 3:11 pm
59[...] Tutorial Link Demo Link [...]
En iyi örneklerle jQuery : “Write less, do more” | enes sönmez - ki?isel blog ?eysi
February 5th, 2010 at 11:24 am
60[...] nas?l çal???yor? jQuery ile kayan menü yap?m? video anlat?m? jQuery ile ayn? anda birden fazla dosya upload arayüzüj Query ile kö?eleri yuvarlat?lm?? [...]
3rbsc.com
February 10th, 2010 at 10:22 am
61good topic
Slava
February 21st, 2010 at 1:23 pm
62Dear Sirs,Administration,
I represent the company SecretsLine LTD, My nfvt is Slava, our company selling VPN accesses.
Do you know that in the U.S. and European country work laws about piracy? People are afraid to download movies and music in the Internet.
I have project https://secretsline.biz/en/partners/ .
Your customers will be able to download movies anonymous.Therefore not be brought to justice.I want to talk to you about partnership.
Our company 3 years, we have large quantity of servers worldwide ,Partner Program for 2 years.
Clients of our service people who shook free music or movies very much, so that your resource has targeted traffic to our service.
When a person is registered to you, he always assigned to you and all the time how much he will pay, you’ll have %.
It is waiting for your response, I hope for long cooperation.
Contact me by ICQ: 769838
Or E-mail: bingoguide@secretsline.biz
Best Regards,
CEO Vyacheslauv Tabola
SecretsLine Ltd.
40??jquery?? | QK31
February 23rd, 2010 at 12:38 pm
63[...] Tutorial Link Demo Link [...]
tommy
March 2nd, 2010 at 3:01 pm
64im looking for this script for long time, and happy when i saw this script on your site
and working for me
PHP-help » Excellent jQuery Tutorials
March 8th, 2010 at 8:13 am
65[...] Tutorial Link Demo Link [...]
35 Ultimate Useful jQuery Tutorials | WEBAXES.COM
May 5th, 2010 at 1:18 am
66[...] Multiple File Upload Magic With Unobtrusive Javascript [...]
37dc.net
May 21st, 2010 at 7:43 am
67good topic
so cool
ric
May 23rd, 2010 at 12:07 pm
68nice, simple and apparently working with no major bugs.
it would be nice to see this article updated and worked on!
Jim
July 1st, 2010 at 2:13 pm
69Anyone got the Multiple file input version working – the demo said there are ‘issues’…
Ramonfer
July 14th, 2010 at 3:30 pm
70Another Free Fast Multi Site File Uploader:
http://www.multisiteupload.com
works well
eric
August 28th, 2010 at 3:20 pm
71Hello,
You can upload multiple files to multiple file hosting sites in EmbedUpload.com .
Just try it.
widik
August 31st, 2010 at 12:07 am
72i can’t found link demo.
“file not found”
daniel
September 2nd, 2010 at 5:52 pm
73very good
Nick Kirkes
November 20th, 2010 at 10:12 am
74Anyone know where the actual examples for this went? Would love to see them…
AntonioF
November 25th, 2010 at 1:27 pm
75The examples are at this address
http://the-stickman.com/web-development/javascript/upload-multiple-files-with-a-single-file-element/
John
January 20th, 2011 at 8:02 pm
76It might not be a bad idea for you to update all the dead demo links in your tutorials posted at the jQuery site…
http://docs.jquery.com/Tutorials:Multiple_File_Upload_Magic
46 Excellent jQuery Tutorials For Web Developers
February 15th, 2011 at 2:53 am
77[...] 4. Multiple File Upload Magic With Unobtrusive Javascript [...]
hm2k
February 16th, 2011 at 11:46 am
78Demos are dead, can we see them back online?
Ta.
Alex
March 2nd, 2011 at 2:17 am
79Hey mate,
looks like you’ve put a lot of work into this, any chance we can view the tutorials/demos?
Cheers,
Alex.
Danny
March 13th, 2011 at 12:32 am
80yes. demos would be nice. thanks anyway.
Late Night Stop 45 Great JQuery Tutorials !
March 16th, 2011 at 6:45 pm
81[...] Tutorial Link Demo Link [...]
Review
April 17th, 2011 at 10:47 am
82How to return the folder and the filename back to the main side from the iframe , after the file is uploaded? I am trying to use the jquery iframe to upload a file and return the folder and filename to a textbox that is used as an custom field in wordpress. – Does somebody know any good video tutorial ? – Thank you!
Genevive Fredenburg
May 31st, 2011 at 3:59 pm
83Great content , glad I found your link , to get off a little bit of the topic I am Using Host Gator and Zen Cart. I go to my Admin page and click on “whos online”. I see that someone is using my site. Admin tells me that they logged on 5 minutes ago (for example). Then it also has a place that shows “time since clicked”. I assume that means “time since the last click on my site”. But in most cases “last click” is the same as the total time on the site. I will bookmark your site. Cheers
Jason
August 23rd, 2011 at 9:21 am
84Would love to see the tutorials and demos. Can they be revived???
40+ Excellent jQuery Tutorials « RPL Class
October 2nd, 2011 at 9:58 pm
85[...] Tutorial Link Demo Link [...]
vajinismus
October 8th, 2011 at 7:38 am
86thanks…
engi
November 12th, 2011 at 11:50 am
87This is great for straight up PHP developed sites, but what about applications like drupal, wordpress, or joomla? where there are harder constraints surrounding the implementation of jQuery? Have you tried implementing this on any of these?
Toko Bunga
December 5th, 2011 at 8:53 am
88thanx
Michael Phelps diet
December 7th, 2011 at 6:34 am
89Between me and my husband we’ve owned more MP3 players over the years than I can count, including Sansas, iRivers, iPods (classic & touch), the Ibiza Rhapsody, etc. But, the last few years I’ve settled down to one line of players. Why? Because I was happy to discover how well-designed and fun to use the underappreciated (and widely mocked) Zunes are.
website tips
December 19th, 2011 at 4:00 am
90the demo links is dead. How can i see ?
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Links
15 Days Of jQuery is proudly powered by WordPress - BloggingPro theme by: Design Disease