I'm trying to create a form where I can have multiple file upload sections, where the user can upload multiple files.
That part, is reasonably straight forward. My problem comes from allowing the user to 'remove' a file from the upload list, before it's uploaded.
I've created a fiddle to illustrate
http://jsfiddle.net/alexjamesbrown/o62srbew/
I've got a simple row, that holds the <input type="file"
<div class="row files" id="files1"> <h2>Files 1</h2> <span class="btn btn-default btn-file"> Browse <input type="file" name="files1" multiple /> </span> <br /> <ul class="fileList"></ul> </div> then, so far, I've created a jquery plugin so it's re-usable:
$.fn.fileUploader = function (filesToUpload) { this.closest(".files").change(function (evt) { for (var i = 0; i < evt.target.files.length; i++) { filesToUpload.push(evt.target.files[i]); }; var output = []; for (var i = 0, f; f = evt.target.files[i]; i++) { var removeLink = "<a href=\"#\" data-fileid=\"" + i + "\">Remove</a>"; output.push("<li><strong>", escape(f.name), "</strong> - ", f.size, " bytes. ", removeLink, "</li> "); } $(this).children(".fileList") .append(output.join("")); }); }; I'm then initialising my very basic plugin like this:
var filesToUpload = []; $("#files1").fileUploader(filesToUpload); $("#files2").fileUploader(filesToUpload); $("#uploadBtn").click(function (e) { e.preventDefault(); });