Ajax发送多个base64图像

Ajax send multiple base64 image

本文关键字:base64 图像 Ajax      更新时间:2023-09-26

我通过ajax请求发送多个图像。但是由于内存问题,发送多个图像会有问题。

当我尝试json_decode这个对象thumbnailArray。我得到了这个

致命错误:允许的内存大小为134217728字节耗尽(已尝试)分配1048655字节)

thumbnailArray = {
    image1 : {
        image : base64,
        rotate : 0,
        order : 1
    },
    image2 : {
        image : base64,
        rotate : 0,
        order : 2
    }
}

我发送base64图像在对象与一些值,如图像旋转和顺序。这是代码:

    var formData = new FormData( $(this)[0] );
    formData.append('ab-user-image', JSON.stringify(thumbnailArray));
    $.ajax({
        url : baseUrl+'user/ajax_user_add/',
        type : 'POST',
        data : formData,
        async : true,
        cache : false,
        contentType : false,
        processData : false,
        success : function(data) {
            //success
        }
    });

是否有更好的方法来发送base64图像或改进此代码或其他东西?

您必须在php.ini中增加限制:

upload_max_filesizepost_max_size

或重新创建ajax函数以对每个图像

进行1次调用

您可以像这样重建ajax函数:

var formData
 = new FormData( $(this)[0] );
    formData.append('ab-user-image', JSON.stringify(thumbnailArray));
    $(formData).each().ajax({
        url : baseUrl+'user/ajax_user_add/',
        type : 'POST',
        data : this,
        async : true,
        cache : false,
        contentType : false,
        processData : false,
        success : function(data) {
            //success
        }
    });

我就是这么做的

var picReader = new FileReader();
picReader.onload = function(event){
    $('.ab-image-upload .ab-image-uploaded').append(createContainerThumbnail(event.target.result, j));
    thumbnailArray['ab-user-image'+j] = {
         image: event.target.result,
         rotate: 0,
         order: 0
    };
    j++;
};
picReader.readAsDataURL(file);
if(formData) {
    formData.append('images[]', file);
}

我追加在formData文件直接在FileReader。然后通过ajax发送