HTML 离线应用程序缓存,列出下载的文件

发布于 2024-10-19 09:37:10 字数 436 浏览 4 评论 0原文

作为我正在构建的离线 Web 应用程序加载屏幕的一部分(使用缓存清单),我想显示一个准确的进度条,让用户知道哪些文件到目前为止已下载,哪些文件仍待下载。类似以下内容:

Loading...
/assets/images/logo.png: loaded
/assets/images/splashImage.png: pending

我知道我可以使用缓存“待处理”事件,但我没有看到事件参数有任何与之关联的数据。

有什么办法可以做到这一点吗?

As part of a loading screen for an offline-enabled web application I'm building (using a cache manifest), I'd like to display an accurate progress bar that lets users know which files has thus far been downloaded and which are still pending. Something like the following:

Loading...
/assets/images/logo.png: loaded
/assets/images/splashImage.png: pending

I know that I can use the cache "pending" event, but I don't see that the event arguments have any data associated with them.

Is there any way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漆黑的白昼 2024-10-26 09:37:10

下载每个文件时都会触发一个 progress 事件,但其负载不包含我测试过的任何浏览器(Chrome、Safari、FF beta)中的文件名。 Chrome 在控制台中显示文件名(尽管据我所知 JS 无法访问),但 Safari 和 FF 都没有做到这一点。据我所知,这些文件的下载顺序与清单中列出的顺序不同,因此甚至没有一种方法可以生成有序列表,然后一次将它们删除。

所以回答你的问题,不,现在没有任何办法可以做到这一点。将来 progress 事件可能会包含文件名 - 至少在某些浏览器中 - 但现在这是不可能的。

我应该补充一点,在 Chrome 中(而不是在 Safari 或 FF 中)你至少可以得到要下载的文件的数量,让你至少可以计算出准确的进度条。要在 Chrome 中获得此功能,您可以使用以下命令:

function downloadProgress(e) {
    totalfiles = Number(e.total);
}
window.applicationCache.addEventListener("progress", downloadProgress, false);

但是,这在其他浏览器中会出错,因此您需要包装 try/catch 或其他一些方法 (typeof(e.total )) 以避免错误。

There is a progress event that gets triggered when each file downloads, however its payload does not include the file name in any browser that I've tested with (Chrome, Safari, FF beta). Chrome displays the file name in the Console (though as far as I know it's inaccessible to JS), but neither Safari nor FF even go that far. And from what I've seen, the files do not download in the same order that they're listed in the manifest, so there's not even a way to generate an ordered list then knock them off one at a time.

So in answer to your question, no, there isn't any way to do this right now. It's possible that in the future the progress event will include the filename - at least in some browsers - but right now this isn't possible.

I should add that in Chrome (not in Safari or FF) you can at least get a count of files to be downloaded, allowing you to at least calculate an accurate progress bar. To get this in Chrome you'd use the following:

function downloadProgress(e) {
    totalfiles = Number(e.total);
}
window.applicationCache.addEventListener("progress", downloadProgress, false);

However this will error out in other browsers, so you need to wrap a try/catch or some other method (typeof(e.total)) to avoid the error.

初心 2024-10-26 09:37:10

虽然晚了几年,但也许它会对其他正在研究这个问题的人有所帮助。
它不会列出文件或任何内容,但它会根据加载的文件总数显示准确的进度条。可能还需要一些工作......
https://github.com/joelabeyta/app-cache-percent-bar

This is a few years late, but maybe it'll help someone else who's researching this.
It doesn't list the files or anything, but it shows an accurate(ish) progress bar based on the total number of files loaded. It may still need a little work...
https://github.com/joelabeyta/app-cache-percent-bar

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文