简体   繁体   中英

AJAX post multiple data after file upload

I have this code that uploads a file with $_FILES :

$('input[type=file]').change(function() {
    var FilesID = $(this).attr('id');
    alert(FilesID);
    var data = new FormData();

    jQuery.each(jQuery("#" + FilesID)[0].files, function(i, file) {
        data.append('file-'+i, file);
    });

    jQuery.ajax({
        url: 'upload/index.php',
        data: data + '&picid=' + FilesID,
        cache: false,
        contentType: false,
        processData: false,
        type: 'POST',
        success: function(data){

            $("#alertspan").text(data);
            document.getElementById("alertbox").style.display = 'block';
            setTimeout(function() {document.getElementById("alertbox").style.display = 'none';},5000);
        }
    });
});

I would like to add POST data on top of the file upload, basically so that in addition to the uploaded file, you have picid equal to the variable FileID .

What about using data.append('picid', FilesID); prior to calling jQuery.ajax() ?

If you're OK with GET instead of POST, just use 'upload/index.php?picid=' + encodeURIComponent(FilesID)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM