Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to construct 'FormData' [closed]

When I upload file in my dropzone, it do not work. Usually it's work very well, but since 1 month I have this JS Error :

Uncaught TypeError: Failed to construct 'FormData': parameter 1 is not of type 'HTMLFormElement'.

That is the code, when I use FormData :

 var form_data = new FormData("#my-awesome-dropzone");

Dopzone code

  Dropzone.options.myAwesomeDropzone = {
    maxFilesize: 5,
    maxFiles: 1,    
    addRemoveLinks: true,
    dictResponseError: 'Server not Configured',
    acceptedFiles: ".pdf",
    init:function(){
      var self = this;
      // config
      self.options.addRemoveLinks = true;
      self.options.dictRemoveFile = "Delete";
      //New file added
      self.on("addedfile", function (file) {
          console.log('new file added ', file);
             if(!confirm("Do you want to upload the file?")){
                this.removeFile(file);
                return false;
            }

      });
      // Send file starts
      self.on("sending", function (file, xhr, formData) {
        console.log('upload started', file);
        $('.meter').show();

            var form_data = new FormData("#my-awesome-dropzone");

            $.ajax({
                url: '/settings/uploadFile', 
                data: 'file=' + file.name ,
                type: 'POST',
                processData: false,
                contentType: false,
                success: function(response) {
                }
            });
      });
      
      // File upload Progress
      self.on("totaluploadprogress", function (progress) {
        console.log("progress ", progress);
        $('.roller').width(progress + '%');
      });

      self.on("queuecomplete", function (progress) {
        $('.meter').delay(999).slideUp(999);
      });
      
      // On removing file
      self.on("removedfile", function (file) {
        console.log(file);
      });
    }

HTML CODE

     <form  enctype="multipart/form-data" action="/settings/uploadFile"  method="post" class="dropzone" 
                        id="my-awesome-dropzone">

         
                     </form> 

Edit 01-08-2019 : Okay, just tested, and it's works on Microsoft Edge 44.17763.1.0 but not on Google Chrome or Firefox, any explanation?

like image 279
MasterSinge Avatar asked Jul 31 '19 08:07

MasterSinge


People also ask

What is a FormData?

The FormData interface provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the fetch() or XMLHttpRequest.send() method. It uses the same format a form would use if the encoding type were set to "multipart/form-data" .

What is FormData object in javascript?

The FormData object lets you compile a set of key/value pairs to send using XMLHttpRequest . It is primarily intended for use in sending form data, but can be used independently from forms in order to transmit keyed data.

What is new FormData ()?

FormData objects are used to capture HTML form and submit it using fetch or another network method. We can either create new FormData(form) from an HTML form, or create an object without a form at all, and then append fields with methods: formData. append(name, value)

How to append object to FormData?

FormData.append() The append() method of the FormData interface appends a new value onto an existing key inside a FormData object, or adds the key if it does not already exist.


1 Answers

You're passing a string to FormData. As the error says, it expects a form element, not a string. So:

var form_data = new FormData(document.getElementById("my-awesome-dropzone"));

Live Example:

var data = new FormData(document.getElementById("my-awesome-dropzone"));
console.log("Created FormData, " + [...data.keys()].length + " keys in data");
<form  enctype="multipart/form-data" action="/settings/uploadFile"  method="post" class="dropzone" id="my-awesome-dropzone">
<input type="text" name="x" value="kittens">
<input type="text" name="y" value="puppies">
</form>
like image 169
T.J. Crowder Avatar answered Oct 17 '22 09:10

T.J. Crowder