This is something I fear has been answered before, but I couldn't find anything.
I am working on a modest web application. Part of it is a file-uploader which uploads xml-files to a Java servlet, which are parsed and imported into a database.
Until recently, the html and javascript enabling this looked something like the following (Note that the ironic and/or unhelpful comments are put in place of large chunks of code irrelevant to the question):
html
<form id="import_form">
<!-- Some irrelevant divs here -->
<span class="btn btn-default btn-file">
Browse <input name="filesToUpload[]" id="filesToUpload" type="file" multiple="" accept="" onchange="updateFileList();"/>
</span>
<!-- Some irrelevant divs here -->
</form>
Javascript
function submitImport() {
var formData = new FormData($('#import_form')[0]);
$.ajax({
url : "uploadEndpoint",
type : "POST",
data : formData,
/* Some more elements */
success : function(response) {
// Handle success, it ain't easy
},
error : function(request, error, status){
// Handle failure, weep
}
});
The submitImport
method is called when the user clicks himself/herself through a set of buttons.
This is all fine and dandy, and works just fine. However, in an effort to make the web application cool and stuff, today, I tried to add a 'drop-zone':
html
<div id="drop-zone">
<p class="info">Or drop files here</p>
</div>
Javascript
// Handler for the drop-zone
$('#drop-zone').on('dragover', function(event){
event.preventDefault();
event.stopPropagation();
if(!event.dataTransfer){
event.dataTransfer = event.originalEvent.dataTransfer;
}
event.dataTransfer.dropEffect = "copy";
$(this).css("background-color", "#f5f5f5");
}).on('dragleave', function(event){
event.preventDefault();
event.stopPropagation();
$(this).css("background-color", "#fff");
}).on('drop', function(event){
event.preventDefault();
event.stopPropagation();
if(!event.dataTransfer){
event.dataTransfer = event.originalEvent.dataTransfer;
}
var files = event.dataTransfer.files;
// Okey so what now?
});
Still with me? Okey, so my question is the following:
How do I in one chunk send, via $.ajax
, the files added both from the "Browse"-input, and the drop-zone?
As indicated by the Okey so what now?
comment, I don't really know what to do with the files added in the drop-zone. I can easily fetch the "Browse"-input-form files with
$("#filesToUpload").prop("files");
However, I've come to understand that FileLists are immutable, so that doesn't really help me much, as I cannot append the first to the second.
The one chunk part is vital for application-specific reasons. Consequently, solutions like sequentially sending all FileLists are not ideal.
Note that I am quite the novice in anything web related, so apologies if the answer is staring me in the face.