使用 Javascript 中的 XMLHttpRequest 暂停下载 [英] Pause a download with XMLHttpRequest in Javascript

查看:35
本文介绍了使用 Javascript 中的 XMLHttpRequest 暂停下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有 Javascript 的 XMLHttpRequest 对象的网络链接下载文件.我无法确定是否可以暂停下载然后恢复它?

I am trying to download a file using web link with Javascript's XMLHttpRequest Object.I am not able to find out whether is it possible to pause a download and later resume it?

推荐答案

技术上是可以的,无需暂停/恢复,您只需执行一个响应类型为blob"的 XmlHttpRequest,然后通过链接将 blob 转发给用户(通过点击像 mega 即时下载)

Technically it's possible, Without pause / resume you could just do a XmlHttpRequest with responseType "blob" and then forward the blob to the user via a link (instant download via click like mega)

但是如果你想暂停/恢复我读过的你必须使用 XmlHttpRequest 的方法 abort,创建一个临时 blob,当用户恢复下载时,你需要从你之前请求的字节开始一个新的 xhr停止.完成下载(或发生新的中止)后,您需要将所有新字节添加到前一个 blob 中.

But if you want a pause / resume i've read you must use the method abort of the XmlHttpRequest, create a temporary blob and when the user resume the download you need to start a new xhr from the byte where your previous request stop. After finishing the download (or a new abort occurs) you need to add all the new byte to the previous blob.

我需要尝试一下,但唯一的限制(我认为)可能是浏览器崩溃或 blob 配额超出.

I need to try that, but the only limit (I think) can be a browser crash or a blob quota exed.

对不起我的英语,我是一只法国青蛙;)

Sorry for my english, i'm a french frog ;)

这篇关于使用 Javascript 中的 XMLHttpRequest 暂停下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆