javascript - 为什么使用二进制图像发起 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 报告):
解决方案
推荐阅读
- haskell - 可以为可执行文件和测试建立不同标志的依赖关系吗?
- elasticsearch - elasticsearch 7未能在索引logstash类型事件上放置映射并拒绝映射更新
- python - Icrawler 只工作了 1 次,然后它就不起作用了
- c# - MongoDB 中的 HasData 等价物
- python - 多标签情感分类
- json - 如何使用 QT 读写 JSON 文件
- sql - 选择具有特定值的所有列
- azure - 存储 blob delete-batch 删除所有 blob,不包括具有相似名称的两个目录
- java - 如何在不将业务逻辑放在转换器类中的情况下将 DTO 转换为实体?
- oauth-2.0 - 使用 SSO 保护托管在 openshift 上的应用程序