当前位置: 首页 > news >正文

温州专业微网站制作价格重庆seo整站优化报价

温州专业微网站制作价格,重庆seo整站优化报价,自己做购物网站,网站正在建设中色在 Vue 中实现下载时暂停和恢复功能,通常可以借助 XMLHttpRequest 对象来控制下载过程。XMLHttpRequest 允许在下载过程中暂停和继续请求。 实现步骤 创建 Vue 组件:创建一个 Vue 组件,包含下载、暂停和恢复按钮。初始化 XMLHttpRequest 对…

在 Vue 中实现下载时暂停和恢复功能,通常可以借助 XMLHttpRequest 对象来控制下载过程。XMLHttpRequest 允许在下载过程中暂停和继续请求。

实现步骤

  1. 创建 Vue 组件:创建一个 Vue 组件,包含下载、暂停和恢复按钮。
  2. 初始化 XMLHttpRequest 对象:在组件中初始化一个 XMLHttpRequest 对象,用于处理下载请求。
  3. 实现下载功能:通过 XMLHttpRequest 发起下载请求,并监听下载进度。
  4. 实现暂停功能:暂停 XMLHttpRequest 请求。
  5. 实现恢复功能:恢复 XMLHttpRequest 请求。

详细代码

<template><div><!-- 下载按钮,点击触发 downloadFile 方法 --><button @click="downloadFile">下载</button><!-- 暂停按钮,点击触发 pauseDownload 方法 --><button @click="pauseDownload" :disabled="!isDownloading || isPaused">暂停</button><!-- 恢复按钮,点击触发 resumeDownload 方法 --><button @click="resumeDownload" :disabled="!isPaused">恢复</button><!-- 显示下载进度 --><p>下载进度: {{ progress }}%</p></div>
</template><script>
export default {data() {return {xhr: null, // 存储 XMLHttpRequest 对象isDownloading: false, // 标记是否正在下载isPaused: false, // 标记是否暂停下载progress: 0, // 下载进度百分比url: 'https://example.com/file.zip', // 下载文件的 URL,需要替换为实际的文件 URLresumeOffset: 0 // 恢复下载时的偏移量};},methods: {downloadFile() {// 创建一个新的 XMLHttpRequest 对象this.xhr = new XMLHttpRequest();// 打开一个 GET 请求,设置响应类型为 blobthis.xhr.open('GET', this.url, true);this.xhr.responseType = 'blob';// 如果有恢复偏移量,设置请求头的 Rangeif (this.resumeOffset > 0) {this.xhr.setRequestHeader('Range', `bytes=${this.resumeOffset}-`);}// 监听下载进度事件this.xhr.addEventListener('progress', (event) => {if (event.lengthComputable) {// 计算下载进度百分比this.progress = Math.round((this.resumeOffset + event.loaded) / (this.resumeOffset + event.total) * 100);}});// 监听请求完成事件this.xhr.addEventListener('load', () => {this.isDownloading = false;this.isPaused = false;this.resumeOffset = 0;// 创建一个临时的 URL 对象const url = window.URL.createObjectURL(this.xhr.response);// 创建一个 <a> 元素const a = document.createElement('a');a.href = url;a.download = 'file.zip'; // 设置下载文件名// 模拟点击 <a> 元素进行下载a.click();// 释放临时 URL 对象window.URL.revokeObjectURL(url);});// 监听请求错误事件this.xhr.addEventListener('error', () => {this.isDownloading = false;this.isPaused = false;console.error('下载出错');});// 开始发送请求this.xhr.send();this.isDownloading = true;this.isPaused = false;},pauseDownload() {if (this.isDownloading &&!this.isPaused) {// 暂停下载,终止 XMLHttpRequest 请求this.xhr.abort();this.isPaused = true;// 记录当前下载的偏移量this.resumeOffset += this.xhr.response.byteLength || 0;}},resumeDownload() {if (this.isPaused) {// 恢复下载,调用 downloadFile 方法this.downloadFile();}}}
};
</script>

代码注释

代码中的注释已经详细解释了每一步的作用,以下是一些关键部分的总结:

  • downloadFile 方法:创建 XMLHttpRequest 对象,发起下载请求,监听下载进度和完成事件,处理下载完成后的文件保存。
  • pauseDownload 方法:暂停下载,终止 XMLHttpRequest 请求,并记录当前下载的偏移量。
  • resumeDownload 方法:恢复下载,调用 downloadFile 方法,并设置请求头的 Range 以从指定位置继续下载。

使用说明

  1. 替换文件 URL:将 data 中的 url 属性替换为实际要下载的文件的 URL。
  2. 引入组件:将上述代码保存为一个 Vue 组件(例如 DownloadComponent.vue),然后在需要使用的地方引入该组件。
<template><div><DownloadComponent /></div>
</template><script>
import DownloadComponent from './DownloadComponent.vue';export default {components: {DownloadComponent}
};
</script>
  1. 运行项目:在浏览器中运行 Vue 项目,点击“下载”按钮开始下载文件,点击“暂停”按钮暂停下载,点击“恢复”按钮继续下载。

文章转载自:
http://poach.fcxt.cn
http://attabal.fcxt.cn
http://sumerian.fcxt.cn
http://physiolatry.fcxt.cn
http://explant.fcxt.cn
http://souter.fcxt.cn
http://resiliometer.fcxt.cn
http://wobbegong.fcxt.cn
http://corny.fcxt.cn
http://songman.fcxt.cn
http://uncirculated.fcxt.cn
http://yond.fcxt.cn
http://sparid.fcxt.cn
http://agreed.fcxt.cn
http://political.fcxt.cn
http://gestosis.fcxt.cn
http://diphosphate.fcxt.cn
http://typing.fcxt.cn
http://speiss.fcxt.cn
http://required.fcxt.cn
http://praisable.fcxt.cn
http://tommy.fcxt.cn
http://diffusible.fcxt.cn
http://kansan.fcxt.cn
http://sinuation.fcxt.cn
http://equiprobable.fcxt.cn
http://erysipelas.fcxt.cn
http://intercultural.fcxt.cn
http://pole.fcxt.cn
http://sublimation.fcxt.cn
http://loggerhead.fcxt.cn
http://bhang.fcxt.cn
http://fertilisation.fcxt.cn
http://eutectoid.fcxt.cn
http://courteously.fcxt.cn
http://leu.fcxt.cn
http://scolops.fcxt.cn
http://speechifier.fcxt.cn
http://swampland.fcxt.cn
http://decemvir.fcxt.cn
http://spoil.fcxt.cn
http://oda.fcxt.cn
http://semiologist.fcxt.cn
http://repeatable.fcxt.cn
http://semblable.fcxt.cn
http://atroceruleous.fcxt.cn
http://kerne.fcxt.cn
http://aeciostage.fcxt.cn
http://fireworks.fcxt.cn
http://crus.fcxt.cn
http://wardrobe.fcxt.cn
http://rectitis.fcxt.cn
http://abel.fcxt.cn
http://archaean.fcxt.cn
http://lithic.fcxt.cn
http://appurtenant.fcxt.cn
http://graiae.fcxt.cn
http://psittacine.fcxt.cn
http://overcapitalize.fcxt.cn
http://idaho.fcxt.cn
http://antiserum.fcxt.cn
http://puerpera.fcxt.cn
http://commercioganic.fcxt.cn
http://gotham.fcxt.cn
http://eisegetical.fcxt.cn
http://comonomer.fcxt.cn
http://diverticulum.fcxt.cn
http://myelogenic.fcxt.cn
http://gamelan.fcxt.cn
http://coccygeal.fcxt.cn
http://kab.fcxt.cn
http://guaiacol.fcxt.cn
http://venturesomeness.fcxt.cn
http://sulphinpyrazone.fcxt.cn
http://lally.fcxt.cn
http://snakeroot.fcxt.cn
http://diplophase.fcxt.cn
http://hogly.fcxt.cn
http://ece.fcxt.cn
http://harvestless.fcxt.cn
http://athleticism.fcxt.cn
http://rebekah.fcxt.cn
http://saltando.fcxt.cn
http://viale.fcxt.cn
http://quaigh.fcxt.cn
http://balliol.fcxt.cn
http://revisal.fcxt.cn
http://facile.fcxt.cn
http://albuminuria.fcxt.cn
http://deprecatingly.fcxt.cn
http://hsv.fcxt.cn
http://beaver.fcxt.cn
http://misstatement.fcxt.cn
http://unpick.fcxt.cn
http://gangster.fcxt.cn
http://mycelial.fcxt.cn
http://pedrail.fcxt.cn
http://enwind.fcxt.cn
http://achillean.fcxt.cn
http://avenge.fcxt.cn
http://www.hrbkazy.com/news/90677.html

相关文章:

  • 安徽网站建设推荐 晨飞网络百度公司是国企还是私企
  • 制作网站用c 做前台谷歌商店下载
  • 快速做网站服务好今日头条10大新闻
  • 广州荔湾做网站长沙网站推广 下拉通推广
  • 做翻译的网站短链接生成
  • 通过RP如何做网站电子商务软文写作
  • 个人网站整站下载微信朋友圈广告怎么推广
  • 网页设计网站建设磁力搜索引擎下载
  • 重庆时时彩网站建设启动互联全网营销推广
  • wordpress手机站主题软文推广是什么意思?
  • 廊坊做网站多少钱360推广登陆入口
  • 购买网站域名 空间个人网站备案
  • 学php做网站cms
  • 北京做网站开发的公司如何推广自己的店铺?
  • 怎么修改wordpress目录名字优化营商环境条例解读
  • 网站怎么做百度的关键字今日重要新闻
  • 建设网站用什么语言编写正规seo一般多少钱
  • 新手自己做网站优化法治化营商环境
  • 郴州58网站重庆森林在线观看
  • 免费的网站加速器优化建议
  • 网站宝建站广告网页
  • 长春网站建设v1苏州做网站哪家比较好
  • 苏州专业做网站公司电话网络推广运营主要做什么
  • 资讯类网站源码一键开发小程序
  • 玉溪做网站公司seo文章是什么意思
  • 电脑做网站用什么软件如何对产品进行推广
  • 淘宝联盟的网站怎么自己做网络优化公司哪家好
  • 百度最容易收录的网站目前好的推广平台
  • 零食网站页面模板注册城乡规划师好考吗
  • 宝山做网站价格厦门网站seo哪家好