On a page I have multiple file uploads and each has its own Upload button.
When i call the type=”submit” button each file load goes off to its own controller and its file is loaded. This works nicely and the same code can be used for all file uploads $("form").submit(function (eventObj)
What i am struggling with is this: I don’t want the user to be able to press the Upload button if they have not selected a file, so i have disabled the button until a file is pressed, the disabling bit works fine. What i need help with is how when the file is selected do i call something that changes the disabled button to enabled (when user selects a file) and works for multiple file uploads using the same bit of code that can be used for all file uploads.
Is there a way to get the enable button to work a bit like this line: $("form").submit(function (eventObj) {
which handles all file uploads?
I want to try to avoid having different bits of code for each file upload and keeping names like id=”filename” the same for every file upload. I can get it working for a single file upload but not multiple.
Hopefully my question makes sense, I am newish to Javascript.
Thank you in advance.
<h1>Upload Files</h1>
<form enctype="multipart/form-data" asp-action="UploadFile" asp-controller="Ls" method="post">
<dl>
<dd>
<input type="file" name="file" id="filename">
</dd>
</dl>
<input class="btn btn-primary" id="uploadButton" type="submit" value="Upload" disabled/>
</form>
<form enctype="multipart/form-data" asp-action="UploadFile" asp-controller="Cv" method="post">
<dl>
<dd>
<input name="file" type="file" id="filename">
</dd>
</dl>
<input class="btn btn-primary" id="uploadButton" type="submit" value="Upload" disabled/>
</form>
//there are more of these upload forms
@section Scripts {
<script>
filename.onchange = function(eventObj) {
//this works but only for the first file upload not the other ones.
if($(this).val())
{
$("#uploadButton").prop("disabled", false);
}
else
{
$("#uploadButton").prop("disabled", true);
}
};
$("form").submit(function (eventObj) {
//some stuff
var formData = new FormData(eventObj.currentTarget);
$.ajax({
url : eventObj.currentTarget.action,
type: "POST",
data : formData,
processData: false,
contentType: false,
cache: false,
enctype: 'multipart/form-data',
success:function(data, textStatus, jqXHR){
console.log("in success");
},
error: function(jqXHR, textStatus, errorThrown){
}
});
return false;
});
</script>
}