2

This works when submitting the form directly. Perhaps I am not passing my "form" object to FormData correctly. Laravel is saying that "file" isn't being passed and when I console.log(formData), I'm seeing an object containing the proto prop but as far as I can tell none of my fields

HTML

<form enctype="multipart/form-data" accept-charset="utf-8" method="POST" action="/file">
<input id="file" type="file" name="file">
<button type="submit">Upload</button>
</form>

JS

$('.file-upload-form').submit(function (e) {
    e.preventDefault();
    submitUploadFileForm($(this)); //also tried just passing this without wrapper
});
function submitUploadFileForm(form){
    console.log(form);
    var formData = new FormData(form); //Needed for passing file
    console.log(formData);
    $.ajax({
        type: 'post',
        url: '/file',
        data: formData,
        success: function () {
            alert('done');
        },
        processData: false,
        contentType: false
    });
}
kilrizzy
  • 2,785
  • 6
  • 37
  • 61
  • `FormData` accepts a `form` DOMElement, not a jQuery object. You need to call `submitUploadFileForm()` just passing the `this` reference to the form. You say you've already tried this, if so what was the error? – Rory McCrossan Dec 23 '15 at 15:53
  • :/ maybe I rushed through testing too fast. Setting it back to just "this" worked haha thanks. If you want to throw this in as an answer I can accept it – kilrizzy Dec 23 '15 at 15:56
  • No problem, added it for you. – Rory McCrossan Dec 23 '15 at 15:56

1 Answers1

4

FormData accepts a form DOMElement, not a jQuery object. You need to call submitUploadFileForm() just passing the this reference to the form:

submitUploadFileForm(this);
Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303