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
    });
}
3
  • 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? Commented Dec 23, 2015 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 Commented Dec 23, 2015 at 15:56
  • No problem, added it for you. Commented Dec 23, 2015 at 15:56

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.