简体   繁体   English

将对文件的引用存储在浏览器的本地存储中

[英]Store reference to a file on disk in a browser's localstorage

It seems with the current File API, the only way to access a previously added file is to store the entire File Object in local-storage. 在当前的File API看来,访问以前添加的文件的唯一方法是将整个File Object存储在本地存储中。 This can quickly eat away at the browsers memory when multiple files are added. 添加多个文件时,这会很快吞噬浏览器的内存。

I was wondering if there is a way to only store some type of reference to the file in local-storage and be able to access that file with its reference. 我想知道是否有一种方法可以只在本地存储中存储对该文件的某种类型的引用,并能够使用其引用来访问该文件。

I tried using the URL.createObjectURL function to generate a URL for the file but that URL is tied to the document and expires when the page is reloaded. 我尝试使用URL.createObjectURL函数生成文件的URL,但该URL与文档绑定,并在重新加载页面时过期。 Does any alternative exist? 是否有其他选择?

By "quickly eat away at the browser's memory", I'm assuming you mean that your quota for LocalStorage is being used up by the files you're storing there. 通过“快速吞噬浏览器的内存”,我假设您的意思是您存储在本地存储中的文件已用完LocalStorage的配额。 The local storage quotas are there to prevent websites from using up too much of a user's local disk space. 本地存储配额可以防止网站占用过多的用户本地磁盘空间。 By default, most browsers limit the storage to 5MB per domain. 默认情况下,大多数浏览器将每个域的存储限制为5MB。

From the w3 spec , the local storage quota is actually a feature to prevent what you're asking from happening (ie a website storing more files on a user's disk than the user has allowed quota for): 从w3规范来看 ,本地存储配额实际上是一项功能,可以防止您要的事情发生(即,网站在用户磁盘上存储的文件数量超过了用户允许的配额):

User agents should limit the total amount of space allowed for storage areas, because hostile authors could otherwise use this feature to exhaust the user's available disk space. 用户代理应该限制存储区域所允许的空间总量,因为敌对的作者可能会使用此功能来耗尽用户的可用磁盘空间。

User agents should guard against sites storing data under their origin's other affiliated sites, eg storing up to the limit in a1.example.com, a2.example.com, a3.example.com, etc, circumventing the main example.com storage limit. 用户代理应防止站点在其原始站点的其他关联站点下存储数据,例如,在a1.example.com,a2.example.com,a3.example.com等网站中存储上限,以避开主要的example.com存储限制。

User agents may prompt the user when quotas are reached, allowing the user to grant a site more space. 当达到配额时,用户代理可能会提示用户,从而允许用户向站点授予更多空间。 This enables sites to store many user-created documents on the user's computer, for instance. 例如,这使站点可以在用户的​​计算机上存储许多用户创建的文档。

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

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