AJAX在文件上传后发布多个数据

时间:2015-05-20 02:39:38

标签: javascript php jquery html ajax

我有这个代码用$_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);
        }
    });
});

我想在文件上传之上添加POST数据,基本上这样除了上传的文件之外,您还picid等于变量FileID

1 个答案:

答案 0 :(得分:0)

如果在调用data.append('picid', FilesID);之前使用jQuery.ajax()呢?

如果您使用GET而不是POST,则只需使用'upload/index.php?picid=' + encodeURIComponent(FilesID)