axios请求中断_下载中断和下载进度

点击查看代码
axios请求中断_下载中断和下载进度
<script setup lang="ts">
import axios from 'axios'
import { ref } from 'vue'

let abort: AbortController

const progress = ref(0)

const downloadBtn = () => {
  // 中断控制器
  abort = new AbortController()
  axios({
    method: 'GET', // 请求方式
    url: 'http://localhost:3000/video', // 请求地址
    responseType: 'arraybuffer', // 响应类型
    signal: abort.signal, // 🎉中断控制器标识
    // 下载进度
    onDownloadProgress(e) {
      // 计算进度百分比
      progress.value = Math.round((e.loaded / e.total!) * 100)
    }
  })
    .then((res) => {
      console.log(res)
    })
    .catch(() => {})
}

const abortBtn = () => {
  // 请求中断
  abort.abort()
}
</script>

<template>
  <div>
    <button class="download" @click="downloadBtn">下载视频</button>
    <button class="abort" @click="abortBtn">中止下载</button>
    <div class="progress-bar">
      <div class="progress" :style="{ width: progress + '%' }"></div>
      {{ progress }}%
    </div>
  </div>
</template>

<style scoped>
.progress-bar {
  height: 20px;
  background-color: #eee;
  margin-top: 10px;
}
.progress {
  width: 0%;
  height: 100%;
  background-color: #4caf50;
  transition: width 0.2s linear;
}
</style>

posted @   jialiangzai  阅读(199)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

喜欢请打赏

扫描二维码打赏

微信打赏

点击右上角即可分享
微信分享提示