0

Using the link here...Multiple file upload - with 'remove file' link I was able to basically get this working. However....It deletes the file upload from the screen...but it doesn't remove it from the array so it still gets uploaded. Been playing with this all morning. I am using Django....could it be that the getlist is not being emptied out upon delete? I do notice that when I click remove for multiple files the file count is not decrementing. Which leads me to believe that the remove portion of the Javascript is not working as expected. Thanks in advance for any thoughts...

Javascript..

  $(document).ready(function (){

  $.fn.fileUploader = function (filesToUpload) {
      this.closest(".files").change(function (evt) {

      for (var i = 0; i < evt.target.files.length; i++) {
          filesToUpload.push(evt.target.files[i]);
      };
      var output = [];

      for (var i = 0, f; f = evt.target.files[i]; i++) {
        var removeLink = "<a class=\"removeFile\" href=\"#\" data-fileid=\"" + i + "\">Remove</a>";

                output.push("<li><strong>", escape(f.name), "</strong> - ",
                    f.size, " bytes. &nbsp; &nbsp; ", removeLink, "</li> ");
            }

            $(this).children(".fileList")
                .append(output.join(""));
            });
      };

      var filesToUpload = [];

      $(document).on("click",".removeFile", function(e){
          e.preventDefault();
          var fileName = $(this).parent().children("strong").text();

          for(i = 0; i < filesToUpload.length; ++ i){
              if(filesToUpload[i].name == fileName){
                  filesToUpload.splice(i, 1);
              }
        }
          $(this).parent().remove();
      });

      $("#files1").fileUploader(filesToUpload);
      $("#files2").fileUploader(filesToUpload);

      $("#uploadBtn").click(function (e) {
          e.preventDefault();
      });

    });

HTML....

<div class="row files" id="files1">
    <span class="">
        <input type="file" name="files1" multiple />
    </span>
        <br />
        <ul class="fileList"></ul>
</div> 

I cut down some of the HTML...I used the original version for a bit but it seemed to make no difference. There is a reference in the original SO that it wasn't working properly but then someone indicated they updated the code...but I still can't get it to delete to prevent it from uploading. It comes off the screen fine but when I submit the form the deleted files still get uploaded.

Here's the POST override for Django in a class based CreateView...

def post(self, request, *args, **kwargs):
    if "cancel" in request.POST:
        return HttpResponseRedirect(reverse('Procedures:procedure_main_menu'))
    else:
        self.object = None
        user = request.user
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        file_form = NewProcedureFilesForm(request.POST, request.FILES)
        files = request.FILES.getlist('files1') #field name in model
        if form.is_valid() and file_form.is_valid():
            procedure_instance = form.save(commit=False)
            procedure_instance.user = user
            procedure_instance.save()
            for f in files:
                procedure_file_instance = NewProcedureFiles(attachments=f, new_procedure=procedure_instance)
                procedure_file_instance.save()
            return self.form_valid(form)
        else:
            form_class = self.get_form_class()
            form = self.get_form(form_class)
            file_form = NewProcedureFilesForm()
            return self.form_invalid(form)
Steve Smith
  • 1,019
  • 3
  • 16
  • 38
  • hint about text formatting : `[fancy link name](https://url)` – Cid Sep 16 '21 at 16:33
  • Possible to share the code for upload event handler? – Ritesh Waghela Sep 16 '21 at 16:53
  • @Ritesh Waghela Thanks for the response. I'm using a class based view...a CreateView from Django...uploading the Post override for the multiple files. – Steve Smith Sep 16 '21 at 16:54
  • I do notice that when I click remove for multiple files the file count is not decrementing. Which leads me to believe that the remove portion of the Javascript is not working as expected. – Steve Smith Sep 16 '21 at 17:05
  • I console logged filesToUpload array after removing the files and it seems to be removing the files from the array. – Ritesh Waghela Sep 16 '21 at 17:16
  • Interesting...I'm not sure why the files are still uploading then. If I remove them from the DOM...Django is maybe capturing them somewhere else? – Steve Smith Sep 16 '21 at 17:17
  • How are you passing the filesToUpload array to django post request? – Ritesh Waghela Sep 16 '21 at 17:25
  • I'm researching that now. I believe django does automatically I just need to figure out how that is getting passed in. – Steve Smith Sep 16 '21 at 17:29
  • files = request.FILES.getlist('files1'); this might be the issue, seems it is taking the files from files1 DOM element and not from the array. You need to pass filesToUpload to post request. – Ritesh Waghela Sep 16 '21 at 17:34
  • I'll look into that...filesToUpload as an override to POST? – Steve Smith Sep 16 '21 at 17:40

0 Answers0