如何暂停使用DownloadManager?

发布于 2024-12-02 12:15:40 字数 53 浏览 3 评论 0 原文

我想实现下载时,可以由用户暂停、停止。如何使用 DownloadManager 实现此目的?

I want to achieve that when downloading, it can be paused, stopped by user. How can I achieve this with DownloadManager?

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

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

发布评论

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

评论(3

凡尘雨 2024-12-09 12:15:40

您可以删除下载队列。然后您可以再次启动它,在将其重新添加到队列时传递 Range HTTP 标头(如提到的 TryTryAgain)。我不认为目前可以更容易地做到这一点。

You can remove a download from the queue. Then you can start it up again, passing the Range HTTP Header when re-adding it to the queue (like TryTryAgain mentioned). I don't believe this currently can be done more easily.

世态炎凉 2024-12-09 12:15:40

也许“暂停”对你来说并不完全必要?作为 DownloadManager 实际上 不需要暂停即可恢复。

相反,您可能会因恢复而陷入困境?看看这是否有帮助:

如何恢复中断的下载解决方案的实施位置范围请求标头...

// Open connection to URL.
HttpURLConnection connection =
        (HttpURLConnection) url.openConnection();

// Specify what portion of file to download.
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
// here "downloaded" is the data length already previously downloaded.

// Connect to server.
connection.connect();

除非您实际上需要允许“暂停”(因为切换文件下载优先级、维护列表或其他更复杂的事情)而不仅仅是提供恢复功能,否则应该有所帮助。

It may be that "pausing" isn't exactly necessary for you? As DownloadManager actually doesn't need to pause to be resumed.

Instead you may be getting tripped up with resuming? See if this helps:

how to resume an interrupted download Where the solution is implementing a Range request header...

// Open connection to URL.
HttpURLConnection connection =
        (HttpURLConnection) url.openConnection();

// Specify what portion of file to download.
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
// here "downloaded" is the data length already previously downloaded.

// Connect to server.
connection.connect();

Unless you need to actually allow "pausing" (because of switching file download priority, maintaining a list or something else more complicated) and not just provide resuming capability, that should help.

大海や 2024-12-09 12:15:40

你的意思是在网络应用程序中执行此操作吗?

我也在考虑同样的事情。这也许可以通过 flash 或 sliverlight 等实现,但是在纯 HTML 中呢?

在服务器端,我们可以使用 RandomAccessFileReader(Apache) 或类似的 FileReader(以字节为单位)来访问文件并写入输出流。我们还可以记住用户在上一个会话中下载了多少字节。但问题是没有标准方法将文件保存到客户端本地文件系统中的磁盘。您可以在 IE 中使用 ActiveX 对象写入一些文件,前提是它是顺序(纯文本)文件而不是二进制文件(如 mp3、flv 等)。但这个ActiveX并不是一个解决方案。因此,您可能需要为您的网站设计类似“下载器”的内容。就像 YouTube 下载器一样。

Do you mean to do this in Web Application ?

I am also thinking about the same thing. This might have been possible with flash or sliverlight etc., but in pure HTML?

On the server side we can use RandomAccessFileReader(Apache) or similar FileReader (in bytes) to access file and write to output stream. We can remember how much bytes was downloaded in previous session by the user too. But the problem is there is no standard way to save file to disk in the client's local file system. You can use ActiveX Object in IE to write some file provided it is a sequential (plain text) file and not binary file(like mp3, flv etc). But this ActiveX is not a solution. So you may need to design something like 'Downloader' for you website. Just like Youtube Downloader.

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