I’d love to get the jQuery-file-upload gadget working. It looks wonderful, but it would require some rewriting to get it to do what I need.
And that is, have multiple file-upload widgets across a form. What we’re doing here at the shop is a review in which people from each department have to answer a long questionnaire. Most of the fields are text-entry, but interspersed among them are file uploads for supporting documents.
It’s a long and detailed questionnaire, so we’re assuming that no one is going to do it in one sitting. The jQuery autosave plugin works for most of it, but the file uploads make it necessary to have some kind of asynchronous way to push them up to the server, store them in the right place, and make the right entries to the database.
The jQuery-file-upload plugin would make it so I have to break the single form into multiples, or else put all the file uploads together into one place, which would be confusing.
So Uploadify it is, because it works on the file upload element itself, not the entire form.
Yesterday afternoon I ripped out my old Uploadify code and re-built it from the ground up. Here’s the gist of how it works:
Include the stylesheet:
Pull in the JQuery libraries I’m using:
You may not need all of these of course.
Then the uploadify script itself:
And then whatever your master script is that contains all your functions. I just happen to call it validations because it started out with the controls for form-field validation:
My form fields all have IDs and names that correspond to the section of the survey they fall into, for example “III_att1” which is section 3, first attachment. I need to iterate over all 12 file upload fields and apply the widget. I also have to define three fields based on the HTML itself, because Uploadify doesn’t capture them dynamically. So start with the usual jQuery opener:
And define the three global values:
var submitted = $('#submitted').val();
var submissionid = $('#submissionid').val();
Next, start a loop for all the upload fields:
As I mentioned earlier, each field has a unique ID. We need to grab it:
Then we summon forth the uploadify function and apply it to each. I’ll prepend the pound sign so that jQuery knows what we’re working with:
Then — and this is some of what made me nuts yesterday so I had to build it from scratch — the two main files:
'uploader' : 'uploadify/upload.php',
“upload.php” is going to be your own server-side script for handling the upload submission. They provide a basic example, but you’ll certainly need to do something different. Ours re-names the file, puts it into a directory for the user submitting the form, and a subdirectory for that submission. Then it records the entry in the database.
If you’re really keeping it simple, that’s almost enough. But in this example, you have to provide some of the additional form data so that it knows where to put it. Here we’re picking up the fixed values from the form, and also which form element this actually is so that it can be named and stored accurately on the back-end:
We’re only allowing one file per field:
And only certain file types, which we explain with a helpful block of text:
'fileTypeExts' : '*.doc; *.jpg; *.gif; *.tif; *.tiff; *.png; *.mht; *.pdf; *.ppt; *.xls; *.docx; *.xlsx; *.pptx; *.zip' ,
That by itself will do for a simple implementation. Some things like status bars and success messages are built in, but I want to add a little more. The ‘onUploadSuccess’ and ‘onUploadError’ methods let me do it.
Adjacent to each file upload block there is an additional empty span with an ID of, for example, “III_att1note”. There’s also a hidden field, which we use for form validation (since Uploadify removes the original upload field and replaces it with a Flash movie there’s no static input:file for it to check, so we have to make one) named something like “III_att1_s” (“s” for success!).
So our onUploadSuccess function checks the upload script’s return value, which should be something like “cleanname=theNameOfTheFileYouUploaded”. If we get that, we’ll modify the “note” and “_s” fields accordingly:
$('#' + myinputfield + 'note').text('(' + cleanname + " has been uploaded. Click on the button to replace it.)");
$('#' + myinputfield + '_s').val(cleanname);
And then a server error catcher:
$('#' + myinputfield + 'note').text('(Error: no file has been uploaded. Click on the button to try again. Error returned is ' + RegExp.$1 + "." );
We also need to trap browser or network errors. This is kind of dumbed-down:
alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
Then close our loops and the document.ready function: