首页 > 解决方案 > 为什么使用二进制图像发起 jQuery POST 请求比使用 Base64 编码的等价物要慢?

问题描述

我们需要将图像数据从 Web 客户端发送到服务器。

最初,我们使用 JSON 来 POST 数据并使用 Base64 对图像进行编码。这将图像大小增加了 33%。

即使图像尺寸较小,以二进制形式发送相同的图像数据也需要更多时间来启动请求。

重申一下,这不是关于完整的请求时间,而是关于 jQuery启动请求的时间。(请参阅 Chrome 网络故障、请求发送时间。)

为什么是这样?

如何加速二进制图像数据的 jQuery POST 操作?

jQuery:

// Create request form.
let formData = new FormData();
formData.append('image', imageFile);
formData.append('requestId', '28682');

// Set request settings.
let settings = {
    url: serverUrl,
    method: 'POST',
    timeout: 0,
    contentType: false,
    processData: false,
    data: formData,
    xhr: function() {
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {
           if (xhr.readyState == 2) {
                if (xhr.status == 200) {
                  xhr.responseType = 'blob';
                } else {
                  xhr.responseType = 'text';
                }
           }
        };
        return xhr;
    },
};

请求时间(由 Chrome 报告):

在此处输入图像描述

标签: javascriptjqueryimagepostimage-processing

解决方案


推荐阅读