I have the following markup:
<div class="file-container">
<input type="file" name="file" id="file" />
</div>
I can get the uploaded file in my handler and ajax call as follows:
$(document).on("change", ".file-container :file", function () {
$.ajax({
url: "xyz",
type: "POST",
files: $("form :file"),
processData: false,
dataType: "json"
}).complete(function (data) {
// do stuff
});
});
But lets say i have two file inputs as follows:
<div class="file-container">
<input type="file" name="file1" id="file1" />
</div>
<div class="file-container">
<input type="file" name="file2" id="file2" />
</div>
On file1 change, how do i get the file element of file1 in my jQuery onchange handler and ajax call i.e what do i change this line to:
$(document).on("change", ".file-container :file", function ()
And this line:
files: $("form :file"),
The following doesnt work:
$(document).on("change", ".file-container #file1", function () {
$.ajax({
url: "xyz",
type: "POST",
files: $("form #file1"),
processData: false,
dataType: "json"
}).complete(function (data) {
// do stuff
});
});
$(document).on("change", ".file-container #file2", function () {
$.ajax({
url: "xyz",
type: "POST",
files: $("form #file2"),
processData: false,
dataType: "json"
}).complete(function (data) {
// do stuff
});
});
onchangeevent for each input could be one approach of distinguishing which input was changed.