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 been reading other posts like:

How to disable submit button until file is selected

Enable submit button after uploading more than 1 file

But I'm not able to extrapolate it to my case:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="https://unpkg.com/[email protected]/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">&times;</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/[email protected]/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>

<script>

$(document).ready(
    function(){
        $('input:file').change(
            function(){
                if ($(this).val()) {
                    $('input:submit').attr('disabled',false);; 
                } 
            }
            );
    });

</script>

</body>
</html>

The last script added is the solution given in the first link, which works properly but is not what I want.

The file-uploader code is from https://github.com/promosis/file-upload-with-preview, where I've seen that there is a method called selectedFilesCount that maybe is useful.

Thank you for your time and sorry if you see some nonsense in my code but I'm new with these languages...

1 Answers

1
Woodrow On Best Solutions

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/[email protected]/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">&times;</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/[email protected]/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>