作者:OpenZzzz | 来源:互联网 | 2023-09-02 12:09
I am trying to download a file using AJAX call. I need to use AJAX call because I have to make a post request along with that I need to send some headers from the client. As Server API is not under our control, we don't have much choice other than using AJAX. In order to show file save dialog, I am converting the byte array to blob to Object URL like shown below
我正在尝试使用AJAX调用下载文件。我需要使用AJAX调用,因为我必须发出一个帖子请求以及我需要从客户端发送一些标题。由于Server API不在我们的控制之下,除了使用AJAX之外我们没有太多选择。为了显示文件保存对话框,我将字节数组转换为blob到Object URL,如下所示
var oReq = new XMLHttpRequest();
oReq.open("POST","api/operations/zip", true);
oReq.respOnseType= "arraybuffer";
oReq.Onload= function(oEvent) {
var blob=new Blob([oReq.response], {type: 'application/octet-binary'});
var link=document.createElement('a');
link.href=window.URL.createObjectURL(blob);
link.download="myFileName.zip"; link.click();
}
oReq.send(filePaths);
Now I would like to know is there any limit on Blob size we can have in Javascript other than browser memory constraint. like is it possible to download 4 GB file if I have around 8 GB RAM.
现在我想知道除了浏览器内存约束之外,我们可以在Javascript中使用Blob大小限制。如果我有大约8 GB RAM,可以下载4 GB文件。
2 个解决方案
14
The existing answer appears largely out of date.
现有的答案似乎已经过时了。
Chrome (57+ it appears):
According to Chrome's Blob Storage System Design:
根据Chrome的Blob存储系统设计:
Blob Storage Limits
We calculate the storage limits here.
我们在这里计算存储限制。
In-Memory Storage Limit
- If the architecture is x64 and NOT ChromeOS or Android:
2GB
- 如果架构是x64而不是ChromeOS或Android:2GB
- Otherwise:
total_physical_memory / 5
- 否则:total_physical_memory / 5
Disk Storage Limit
- If ChromeOS:
disk_size / 2
- 如果是ChromeOS:disk_size / 2
- If Android:
disk_size / 20
- 如果是Android:disk_size / 20
- Else:
disk_size / 10
- 否则:disk_size / 10
Note: ChromeOS's disk is part of the user partition, which is separate from the system partition.
注意:ChromeOS的磁盘是用户分区的一部分,该分区与系统分区分开。
Minimum Disk Availability
We limit our disk limit to accomidate a minimum disk availability. The equation we use is:
我们限制磁盘限制以适应最低磁盘可用性。我们使用的等式是:
min_disk_availability = in_memory_limit * 2
min_disk_availability = in_memory_limit * 2
Example Limits
(All sizes in GB)
(所有尺寸均以GB为单位)
Device | Ram | In-Memory Limit | Disk | Disk Limit | Min Disk Availability
----------------+-----+-----------------+------+------------+----------------------
Cast | 0.5 | 0.1 | 0 | 0 | 0
Android Minimal | 0.5 | 0.1 | 8 | 0.4 | 0.2
Android Fat | 2 | 0.4 | 32 | 1.5 | 0.8
CrOS | 2 | 0.4 | 8 | 4 | 0.8
Desktop 32 | 3 | 0.6 | 500 | 50 | 1.2
Desktop 64 | 4 | 2 | 500 | 50 | 4
To clarify, disk_size
is apparently the total disk size, not the available disk space (probably for privacy reasons, but seems like that could cause problems).
为了澄清,disk_size显然是总磁盘大小,而不是可用磁盘空间(可能出于隐私原因,但似乎可能会导致问题)。
Chrome uses a directory called blob_storage
in the Chrome profile directory to store blob parts to disk.
Chrome使用Chrome配置文件目录中名为blob_storage的目录将blob部件存储到磁盘。
I'm not sure if or how some other limits are imposed if available disk space or available RAM are less than the software limits. I'll update this answer when I know more.
如果可用磁盘空间或可用RAM小于软件限制,我不确定是否或如何强加其他限制。当我知道更多时,我会更新这个答案。
Opera (44+ or perhaps earlier):
Same as Chrome, as it is based on Chromium.
与Chrome相同,因为它基于Chromium。
Firefox (53+ or perhaps earlier):
No apparent hard limit. I am able to create Blob's significantly larger than the "800 MiB" FileSaver.js claims. It does not use disk space to back larger blobs, so it all goes in memory, potentially with the operating system paging memory to disk. This means that blobs larger than memory may be possible, though likely with bad performance.
没有明显的硬限制。我能够创建比“800 MiB”FileSaver.js声明大得多的Blob。它不使用磁盘空间来支持更大的blob,因此它全部进入内存,可能是操作系统将内存分页到磁盘。这意味着可能存在大于内存的blob,但可能性能不佳。
Safari (10.1+ or perhaps earlier):
No apparent hard limit. It does not use disk space to back larger blobs, so it all goes in memory, potentially with the operating system paging memory to disk. This means that blobs larger than memory may be possible, though likely with bad performance.
没有明显的硬限制。它不使用磁盘空间来支持更大的blob,因此它全部进入内存,可能是操作系统将内存分页到磁盘。这意味着可能存在大于内存的blob,但可能性能不佳。
Other browser limits will be added as I learn them.
我学习它们时会添加其他浏览器限制。