0

Hi guys. The problem of removing a preview image is solved, but I also need to remove that same image from the input. I tried a lot of ways, but no success. Below is my html and the script that I am using. I don't if this is the best way to do what I'm trying to do. I searched a lot but I lot of people had the same issue, but no answer. I only found a way to remove all the images from input, not the one that I removed from the preview. Hope you guys can save me!.

function handleFileSelect(event) {
  var input = this;
  if (input.files && input.files.length) {
    var filesAmount = input.files.length;
    for (i = 0; i < filesAmount; i++) {
      var reader = new FileReader();
      this.enabled = false
      reader.onload = (function(e) {
        var span = document.createElement('span');
        span.innerHTML = ['<img id="test" class="thumb" src="', e.target.result, '" title="', escape(e.name), '"/><span class="remove_img_preview"></span>'].join('');
        document.getElementById('preview').insertBefore(span, null);
      });
      reader.readAsDataURL(input.files[i]);
    }
  }
}

$('#photos').change(handleFileSelect);

$('#preview').on('click', '.remove_img_preview', function() {
  $(this).parent('span').remove();
  $(this).val("");
});
.thumb {
  width: 80px;
  height: 50px;
  margin: 0.2em -0.7em 0 0;
}

.remove_img_preview {
  position: relative;
  top: -12px;
  right: 7px;
  background: black;
  color: white;
  border-radius: 50px;
  font-size: 0.9em;
  padding: 0 0.3em 0;
  text-align: center;
  cursor: pointer;
}

.remove_img_preview:before {
  content: "×";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form action="{{route('employee.store.settlement')}}" enctype="multipart/form-data" method="POST">


  <div class="form-group">
    <label for="photos">Selecione fotos do povoado</label>
    <input type="file" name="photos[]" id="photos" multiple="multiple" accept="image/*">
  </div>
  <div class="form-group" style="float: right">
    <button type="submit" class="btn btn-success">Cadastrar</button>
  </div>
  <div>
    <style>

    </style>
    <div id="preview"></div>
  </div>
</form>
Thomas
  • 21
  • 4
  • You don't need to wrap the JavaScript part with ` –  May 24 '21 at 21:40
  • @ChrisG I've updated my question, thx for the instrutions. – Thomas May 24 '21 at 21:58
  • Fixed your snippet. There's three areas, one HTML one for the content of ``, one for your CSS rules and one for your JS code. You're also not supposed to have anything outside of or ; putting scripts at the end means put them before `` –  May 24 '21 at 23:43
  • As for the problem of resetting a file input, the easiest way is to remove the input and add a new one. –  May 24 '21 at 23:43

1 Answers1

-1

Bad news I'm afraid. Due to security restrictions, the FileList API s read-only. You can clear it all out through this method: https://stackoverflow.com/a/13351234/1772933

Another ref: https://stackoverflow.com/a/3162319/1772933

Kinglish
  • 23,358
  • 3
  • 22
  • 43
  • Actually you can change [input.files](https://stackoverflow.com/questions/52078853/is-it-possible-to-update-filelist/52079109#52079109) in a roundabout way – Endless May 25 '21 at 13:28