Vue.js實(shí)現(xiàn)下載時(shí)暫?;謴?fù)下載
在 Vue 中實(shí)現(xiàn)下載時(shí)暫停和恢復(fù)功能,通??梢越柚?nbsp;XMLHttpRequest 對象來控制下載過程。XMLHttpRequest 允許在下載過程中暫停和繼續(xù)請求。
實(shí)現(xiàn)步驟
- 創(chuàng)建 Vue 組件:創(chuàng)建一個(gè) Vue 組件,包含下載、暫停和恢復(fù)按鈕。
- 初始化
XMLHttpRequest對象:在組件中初始化一個(gè)XMLHttpRequest對象,用于處理下載請求。 - 實(shí)現(xiàn)下載功能:通過
XMLHttpRequest發(fā)起下載請求,并監(jiān)聽下載進(jìn)度。 - 實(shí)現(xiàn)暫停功能:暫停
XMLHttpRequest請求。 - 實(shí)現(xiàn)恢復(fù)功能:恢復(fù)
XMLHttpRequest請求。
詳細(xì)代碼
<template>
<div>
<!-- 下載按鈕,點(diǎn)擊觸發(fā) downloadFile 方法 -->
<button @click="downloadFile">下載</button>
<!-- 暫停按鈕,點(diǎn)擊觸發(fā) pauseDownload 方法 -->
<button @click="pauseDownload" :disabled="!isDownloading || isPaused">暫停</button>
<!-- 恢復(fù)按鈕,點(diǎn)擊觸發(fā) resumeDownload 方法 -->
<button @click="resumeDownload" :disabled="!isPaused">恢復(fù)</button>
<!-- 顯示下載進(jìn)度 -->
<p>下載進(jìn)度: {{ progress }}%</p>
</div>
</template>
<script>
export default {
data() {
return {
xhr: null, // 存儲(chǔ) XMLHttpRequest 對象
isDownloading: false, // 標(biāo)記是否正在下載
isPaused: false, // 標(biāo)記是否暫停下載
progress: 0, // 下載進(jìn)度百分比
url: 'https://example.com/file.zip', // 下載文件的 URL,需要替換為實(shí)際的文件 URL
resumeOffset: 0 // 恢復(fù)下載時(shí)的偏移量
};
},
methods: {
downloadFile() {
// 創(chuàng)建一個(gè)新的 XMLHttpRequest 對象
this.xhr = new XMLHttpRequest();
// 打開一個(gè) GET 請求,設(shè)置響應(yīng)類型為 blob
this.xhr.open('GET', this.url, true);
this.xhr.responseType = 'blob';
// 如果有恢復(fù)偏移量,設(shè)置請求頭的 Range
if (this.resumeOffset > 0) {
this.xhr.setRequestHeader('Range', `bytes=${this.resumeOffset}-`);
}
// 監(jiān)聽下載進(jìn)度事件
this.xhr.addEventListener('progress', (event) => {
if (event.lengthComputable) {
// 計(jì)算下載進(jìn)度百分比
this.progress = Math.round((this.resumeOffset + event.loaded) / (this.resumeOffset + event.total) * 100);
}
});
// 監(jiān)聽請求完成事件
this.xhr.addEventListener('load', () => {
this.isDownloading = false;
this.isPaused = false;
this.resumeOffset = 0;
// 創(chuàng)建一個(gè)臨時(shí)的 URL 對象
const url = window.URL.createObjectURL(this.xhr.response);
// 創(chuàng)建一個(gè) <a> 元素
const a = document.createElement('a');
a.href = url;
a.download = 'file.zip'; // 設(shè)置下載文件名
// 模擬點(diǎn)擊 <a> 元素進(jìn)行下載
a.click();
// 釋放臨時(shí) URL 對象
window.URL.revokeObjectURL(url);
});
// 監(jiān)聽請求錯(cuò)誤事件
this.xhr.addEventListener('error', () => {
this.isDownloading = false;
this.isPaused = false;
console.error('下載出錯(cuò)');
});
// 開始發(fā)送請求
this.xhr.send();
this.isDownloading = true;
this.isPaused = false;
},
pauseDownload() {
if (this.isDownloading &&!this.isPaused) {
// 暫停下載,終止 XMLHttpRequest 請求
this.xhr.abort();
this.isPaused = true;
// 記錄當(dāng)前下載的偏移量
this.resumeOffset += this.xhr.response.byteLength || 0;
}
},
resumeDownload() {
if (this.isPaused) {
// 恢復(fù)下載,調(diào)用 downloadFile 方法
this.downloadFile();
}
}
}
};
</script>
代碼注釋
代碼中的注釋已經(jīng)詳細(xì)解釋了每一步的作用,以下是一些關(guān)鍵部分的總結(jié):
downloadFile方法:創(chuàng)建XMLHttpRequest對象,發(fā)起下載請求,監(jiān)聽下載進(jìn)度和完成事件,處理下載完成后的文件保存。pauseDownload方法:暫停下載,終止XMLHttpRequest請求,并記錄當(dāng)前下載的偏移量。resumeDownload方法:恢復(fù)下載,調(diào)用downloadFile方法,并設(shè)置請求頭的Range以從指定位置繼續(xù)下載。
使用說明
- 替換文件 URL:將
data中的url屬性替換為實(shí)際要下載的文件的 URL。 - 引入組件:將上述代碼保存為一個(gè) Vue 組件(例如
DownloadComponent.vue),然后在需要使用的地方引入該組件。
<template>
<div>
<DownloadComponent />
</div>
</template>
<script>
import DownloadComponent from './DownloadComponent.vue';
export default {
components: {
DownloadComponent
}
};
</script>
- 運(yùn)行項(xiàng)目:在瀏覽器中運(yùn)行 Vue 項(xiàng)目,點(diǎn)擊“下載”按鈕開始下載文件,點(diǎn)擊“暫停”按鈕暫停下載,點(diǎn)擊“恢復(fù)”按鈕繼續(xù)下載。
到此這篇關(guān)于Vue.js實(shí)現(xiàn)下載時(shí)暫?;謴?fù)下載的文章就介紹到這了,更多相關(guān)Vue.js 下載時(shí)暫?;謴?fù)下載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)點(diǎn)擊追加選中樣式效果
今天小編就為大家分享一篇vue實(shí)現(xiàn)點(diǎn)擊追加選中樣式效果,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
基于Vue3和Element Plus實(shí)現(xiàn)自動(dòng)導(dǎo)入功能
在 Vue 3 項(xiàng)目中,結(jié)合 Element Plus 實(shí)現(xiàn)自動(dòng)導(dǎo)入可以顯著減少代碼量,提升開發(fā)效率,Element Plus 提供了官方的自動(dòng)導(dǎo)入插件 unplugin-vue-components 和 unplugin-auto-import,以下是如何配置和使用的詳細(xì)步驟,需要的朋友可以參考下2025-03-03
vue中computed和watch的使用實(shí)例代碼解析
這篇文章主要介紹了vue中computed和watch的綜合運(yùn)用實(shí)例,主要需求是點(diǎn)擊按鈕實(shí)現(xiàn)天氣的切換效果結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
VUE使用DXFParser組件解析dxf文件生成圖片的操作代碼
這篇文章主要介紹了VUE使用DXFParser組件解析dxf文件生成圖片的操作代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09

