how to do file upload using jquery serialization how to do file upload using jquery serialization php php

how to do file upload using jquery serialization


Use FormData object.It works for any type of form

$(document).on("submit", "form", function(event){    event.preventDefault();    $.ajax({        url: $(this).attr("action"),        type: $(this).attr("method"),        dataType: "JSON",        data: new FormData(this),        processData: false,        contentType: false,        success: function (data, status)        {        },        error: function (xhr, desc, err)        {                    }    });        });


A file cannot be uploaded using AJAX because you cannot access the contents of a file stored on the client computer and send it in the request using javascript. One of the techniques to achieve this is to use hidden iframes. There's a nice jquery form plugin which allows you to AJAXify your forms and it supports file uploads as well. So using this plugin your code will simply look like this:

$(function() {    $('#ifoftheform').ajaxForm(function(result) {        alert('the form was successfully processed');    });});

The plugin automatically takes care of subscribing to the submit event of the form, canceling the default submission, serializing the values, using the proper method and handle file upload fields, ...


   var form = $('#job-request-form')[0];        var formData = new FormData(form);        event.preventDefault();        $.ajax({            url: "/send_resume/", // the endpoint            type: "POST", // http method            processData: false,            contentType: false,            data: formData,

It worked for me! just set processData and contentType False.