Optional File Upload with Blueimp jquery file upload plugin

前端 未结 4 1946
遥遥无期
遥遥无期 2020-12-17 23:51

The question is related to jquery file upload plugin from blueimp

I am building a form where the file uploads are optional. It turns out I cannot post form when the

4条回答
  •  [愿得一人]
    2020-12-18 00:38

    I know I am late to the party, but there was no real solution listed to date. You can fake the fact there is a file being added, by manually calling the add event like:

    $('#fileupload').fileupload('add', { files: [{}] });
    

    You would setup a variable to store the form information, update the variable when add and trigger the add like above if there was no file. Here is what the code would look like:

    var fileData;
    $('#fileupload').fileupload({
        add: function (e, data) {
            fileData = data;
        }
    });
    
    $('form').submit(function () {
        if (!fileData) {
            $('#fileupload').fileupload('add', { files: [{}] });
        }
        fileData.formData = params;
        fileData.submit();
        return false;
    });
    

    This allows you to stay consistent with how the data is passed to the server.

提交回复
热议问题