Search code examples
javascriptjqueryjquery-file-uploadform-data

How do I append a value to formData on event? (blueimp/jQuery-File-Upload)


I have this code for the moment ->

$('#file').fileupload({
    formData: {
        valueThatChangesOverTime: value,
        staticValue: 0
    },
    maxNumberOfFiles: 1,
    autoUpload: false,
    dataType: 'json',
    add: function (e, data) {
        $('#importFilesBtn').on("click", function () {
            // Set value to formData.valueThatChangesOverTime
            data.submit();
        })
    },
    done: function (e, data) {

    }
});

As you see in my comment I want to append a value to the formData object on a click event but I can't figure out how to do it. It always stays with the initial value.

Has someone done this before and how did you solve it? Thanks.


Solution

  • I don't like answering my own questions but for the clarity of this question I thinks it's best. I finally found the solution. Instead of trying to "update" the formData I append it myself in the add: event, like this.

    add: function (e, data) {
            $('#importFilesBtn').on("click", function () {
                data.formData = {
                    valueThatChangesOverTime: myNewValue,
                    staticValue: 0
                };
                data.submit();
            })
        },