how to do file upload using jquery serialization

前端 未结 8 1517
感动是毒
感动是毒 2020-11-22 16:28

So I have a form and I\'m submitting the form through ajax using jquery serialization function

        serialized = $(Forms).serialize();

        $.ajax({
         


        
8条回答
  •  时光取名叫无心
    2020-11-22 17:06

    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, ...

提交回复
热议问题