无法使用formData数组一次上载5张图片

unable to upload 5 pictures at once using formData array

本文关键字:一次 上载 5张 formData 数组      更新时间:2023-09-26

我正在创建一种机制,在输入文件更改时一次上传5张图片。由于某种原因,我无法一次上传5张图片,4张图片可以,5张图片不在同一次尝试也可以,但同时上传5张。等等:(

这是我的代码,我想我犯了一个愚蠢的错误,请帮帮我:(当我调试它时,如果为null,我会发送pictures[]数组,如果我上传4张图片,它就会被填充。

var uploaded_images = 0;
$('#post-upload-pictures').change(function(){
    var formData = new FormData(), pictures = this.files, i;
    uploaded_images = uploaded_images + pictures.length;
    if (pictures.length > 5 || uploaded_images > 5){
        uploaded_images = uploaded_images - pictures.length;
        alert('ניתן להעלות עד חמש תמונות בלבד');
        return;
    }
    for(i=0; i < pictures.length; i++){
        formData.append('pictures['+i+']', pictures[i]);
    }
    if(window.FormData === undefined){
        alert('הדפדפן שאת משתמשת בו ישן מידיי, אנו ממליצים להתקין Chrome');
        return;
    }
    $.ajax({
        url: '/ajax/uploadPictures',
        type: 'POST',
        data: formData,
        dataType: 'json',
        contentType: false,
        processData: false,
        //Ajax events
        beforeSend: function(){
            $('#post-upload i').removeClass('fa fa-camera-retro fa-2x').addClass('spinner');
        },
        success: function(url){
            for(i=0; i < pictures.length; i++){
                $('#post-pictures').append('<img src="' + url.pic[i] +'"/>');
            }
            $('#post-pictures').fadeIn();
            $('#post-upload i').addClass('fa fa-camera-retro fa-2x').removeClass('spinner');
        },
        error: function(){
            $('#post-upload i').addClass('fa fa-camera-retro fa-2x').removeClass('spinner');
        }
    });
});

使用多部分/表单数据作为enctype,并将php.ini中的max_postrongize和max_upload_size更改为大于五个文件的最大文件大小

基本上。。。据我所知。。。您没有使用multipart/form-data内容类型。

所以。。。您的浏览器正在将所有数据作为单个请求发送。

现在。。。在您的服务器上(我认为它在php中)。。。应该存在CCD_ 2的限制。和我认为超过了5个文件。

增加服务器上的maximum-request-size。那应该行得通。

假设php。。。您必须更改php.ini:中的这些特定内容

// Increase as per your expected file-upload size
post_max_size = 30m
// Expected input parsing time... in seconds
max_input_time = 20
// Increase as per your expected execution time of upload script... including input parsing... in seconds
max_execution_time = 30