How To Enable Submit Button Until Condition In A Multiple File Uploader?
I'm doing a WebApp where people will upload 2 images which I need to pass through a python code. For this reason, I only want to let them send it when 2 images are uploaded. I've b
Solution 1:
You can hook into the upload.cachedFileArray
from the upload object to check the array
length to verify if 2 files have been selected for upload. This is checked via a toggle() function that is bound by the window event listeners
fileUploadWithPreview:imageSelected
and fileUploadWithPreview:imageDelete
, so if an image is removed after being selected, you can still enforce the rule of 2:
$(document).ready(function() {
});
var toggle = function() {
//console.log(upload.cachedFileArray.length);
if (upload.cachedFileArray.length == 2) {
$('input:submit').attr('disabled', false);
} else {
$('input:submit').attr('disabled', true);
}
};
window.addEventListener('fileUploadWithPreview:imageSelected', function(e) {
// optionally you could pass the length into the toggle function as a param
// console.log(e.detail.cachedFileArray.length);
toggle();
});
window.addEventListener('fileUploadWithPreview:imageDeleted', function(e) {
// optionally you could pass the length into the toggle function as a param
// console.log(e.detail.cachedFileArray.length);
toggle();
});
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://unpkg.com/file-upload-with-preview@3.4.3/dist/file-upload-with-preview.min.css">
</head>
<body>
<!-- Uploader -->
<div class="custom-file-container" data-upload-id="myImage">
<label>Upload your images <a href="javascript:void(0)" class="custom-file-container__image-clear" title="Clear Image">×</a></label>
<label class="custom-file-container__custom-file">
<form id="upload-form" action="{{url_for('upload')}}" method="POST" enctype="multipart/form-data">
<input type="file" class="custom-file-container__custom-file__custom-file-input" accept="image/*" aria-label="Choose File" multiple>
<input type="hidden" name="MAX_FILE_SIZE" value="10485760" />
<span class="custom-file-container__custom-file__custom-file-control"></span>
</label>
<div class="custom-file-container__image-preview"></div>
</div>
<input type="submit" name="send" disabled/>
</div>
<script src="https://unpkg.com/file-upload-with-preview@3.4.3/dist/file-upload-with-preview.min.js"></script>
<script>
var upload = new FileUploadWithPreview('myImage', {
showDeleteButtonOnImages: true,
text: {
chooseFile: 'Nom del fitxer',
browse: 'Examina'
}
});
</script>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
</body>
</html>
Post a Comment for "How To Enable Submit Button Until Condition In A Multiple File Uploader?"