简体   繁体   English

从XmlHttpRequest转换Blob,而无需将其完全加载到内存中

[英]Transform Blob from XmlHttpRequest without loading it completely into memory

I want to implement a client-side file decryption in the browser. 我想在浏览器中实现客户端文件解密。 It takes decrypted files from a server and should decrypt it in the browser, presenting a Save As dialog to save the decrypted file. 它从服务器获取解密的文件,并应在浏览器中对其进行解密,并显示“ 另存为”对话框以保存解密的文件。 It should also work for large files (1 GB or more). 它也应适用于大文件(1 GB或更大)。

I have the following strategy in mind: 我有以下策略:

  • Download the file using XMLHttpRequest with responseType = 'blob' . 使用XMLHttpRequestresponseType = 'blob'下载文件。
  • Decrypt by transforming the Blob given by XMLHttpRequest . 通过转换XMLHttpRequest给定的Blob进行解密。
  • Provide the decrypted Blob as a objectURL to the user. 向用户提供解密的Blob作为objectURL

The decryption could work like a stream transformation, it reads chunks from the downloaded Blob, decrypts the data and writes into the output Blob. 解密可以像流转换一样工作,它从下载的Blob中读取块,解密数据并将其写入输出Blob。

However as far as I can tell this will only work with current browsers if you can load the whole file into memory (you need to store the complete decrypted blob in memory before you can create an objectURL ). 但是据我所知,这仅适用于当前浏览器,如果您可以将整个文件加载到内存中(您需要在创建objectURL之前将完整的解密blob存储在内存中)。 It seems that no kind of chunked reading/writing is supported by the current Blob , XMLHttpRequest and createObjectURL interface as described on the Mozilla Developer Network . Mozilla开发人员网络上所述,当前的BlobXMLHttpRequestcreateObjectURL接口似乎不支持任何类型的分块读取/写入。 A Blob is immutable and there doesn't seem to be an streaming support for binary data in browsers. Blob是不可变的,并且似乎不存在对浏览器中的二进制数据的流式支持。

Is there any way to implement this with current browsers? 有什么方法可以用当前的浏览器来实现吗?

After getting a Blob with responseType = 'blob'; 在获得带有responseType = 'blob';Blob之后responseType = 'blob'; , you can use blob.slice(start, end) to get a subview and use the FileReader API to grab the data associated with this subview. ,您可以使用blob.slice(start, end)来获得一个子视图,并使用FileReader API来获取与此子视图关联的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM