隨著行動網路的普及,越來越多的應用程式需要上傳文件,如頭像、照片、文件等等。而文件上傳的過程中,使用者往往需要等待一段時間來完成上傳,這時候,進度條就是一個非常好的展示方式。近年來,uniapp成為行動裝置開發的熱門框架之一,本文將介紹如何使用uniapp實現進度條上傳檔案的功能。
一、前知識
在深入學習本文之前,你需要掌握以下技能:
- uniapp的基本使用方法
- ajax非同步請求的使用方法
- 檔案上傳的基本操作
二、準備工作
首先,請確保你已經安裝了vue-cli,然後利用vue-cli建立一個uniapp的專案。因為本文主要講解文件上傳功能的實現,所以將不會涉及其他功能的實現。
三、實作過程
- 建立檔案上傳元件和進度條元件
1.1 建立檔案上傳元件
在uniapp框架中,透過使用uni-upload控制項可以方便地實現檔案上傳的功能。在components資料夾下建立一個Upload元件,程式碼如下:
<template> <view> <uni-upload class="upload-btn" :upload-url="uploadUrl" /> </view> </template> <script> export default { name: "Upload", props: { uploadUrl: { type: String, default: "" } } }; </script> <style lang="scss"> .upload-btn { width: 100px; height: 50px; background-color: #409eff; color: #fff; border: none; border-radius: 4px; text-align: center; line-height: 50px; cursor: pointer; user-select: none; } </style>
1.2 建立進度條元件
利用uniui元件庫中的uni-progress元件可以很方便地實作進度條的功能。在components資料夾下建立一個ProgressBar元件,程式碼如下:
<template> <view> <uni-progress :percent="percent" /> </view> </template> <script> export default { name: "ProgressBar", props: { percent: { type: Number, default: 0 } } }; </script>
- 實作上傳進度條功能
2.1 取得檔案上傳進度
#檔案上傳過程中,伺服器會將上傳進度進行對應的傳回。我們可以透過監聽XMLHttpRequest物件的progress事件來實現上傳進度的取得。在Upload元件中新增以下程式碼:
<template> <view> <uni-upload class="upload-btn" :upload-url="uploadUrl" @change="onChange" /> <ProgressBar :percent="percent" /> </view> </template> <script> import ProgressBar from "../components/ProgressBar"; export default { name: "Upload", props: { uploadUrl: { type: String, default: "" } }, components: { ProgressBar }, data() { return { percent: 0, uploadRequest: null }; }, methods: { onChange(e) { const file = e.target.files[0]; if (!file) return; this.uploadRequest = this.uploadFile(file); }, uploadFile(file) { const formData = new FormData(); formData.append("file", file); const xhr = new XMLHttpRequest(); xhr.open("POST", this.uploadUrl); xhr.upload.addEventListener("progress", this.updateProgress); xhr.send(formData); return xhr; }, updateProgress(e) { const percent = ((e.loaded / e.total) * 100).toFixed(2); this.percent = percent; } } }; </script>
在uploadFile方法中,利用XMLHttpRequest物件建立一個POST請求,並且監聽XMLHttpRequest物件的upload屬性的progress事件。每當上傳事件發生時,updateProgress方法都會被觸發,更新元件中的percent資料。
2.2 取消檔案上傳
在檔案上傳的過程中,使用者可能需要取消上傳操作。為了能夠支援取消操作,我們需要在Upload元件中新增一個取消按鈕,同時也需要在uploadFile方法中加入取消上傳的邏輯。
<template> <view> <uni-upload class="upload-btn" :upload-url="uploadUrl" @change="onChange" /> <ProgressBar :percent="percent" /> <view class="controls"> <view class="btn" @click="cancelUpload">取消上传</view> </view> </view> </template> <script> import ProgressBar from "../components/ProgressBar"; export default { name: "Upload", props: { uploadUrl: { type: String, default: "" } }, components: { ProgressBar }, data() { return { percent: 0, uploadRequest: null }; }, methods: { onChange(e) { const file = e.target.files[0]; if (!file) return; this.uploadRequest = this.uploadFile(file); }, uploadFile(file) { const formData = new FormData(); formData.append("file", file); const xhr = new XMLHttpRequest(); xhr.open("POST", this.uploadUrl); xhr.upload.addEventListener("progress", this.updateProgress); xhr.send(formData); return xhr; }, updateProgress(e) { const percent = ((e.loaded / e.total) * 100).toFixed(2); this.percent = percent; }, cancelUpload() { if (this.uploadRequest) { this.uploadRequest.abort(); } } } }; </script> <style lang="scss"> .controls { margin-top: 10px; } .btn { background-color: #ff4949; color: #fff; width: 100px; height: 30px; text-align: center; line-height: 30px; border-radius: 4px; cursor: pointer; user-select: none; } </style>
當使用者點選取消上傳按鈕時,cancelUpload方法會被執行,此時會透過呼叫XMLHttpRequest物件的abort方法來取消上傳作業。
四、總結
在本文中,我們透過使用uniapp框架結合uniui元件庫中的元件,實作了一個檔案上傳進度條功能。借助XMLHttpRequest物件的onprogress事件,我們能夠及時地取得上傳進度,並且可以透過呼叫XMLHttpRequest物件的abort方法來取消上傳操作。這個小功能不僅可以增加應用程式的使用者體驗,同時也可以幫助開發者更了解XMLHttpRequest物件的使用以及uniapp框架的基本原理。
以上是uniapp實作進度條上傳的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了有關移動和網絡平台的調試策略,突出顯示了Android Studio,Xcode和Chrome DevTools等工具,以及在OS和性能優化的一致結果的技術。

文章討論了用於Uniapp開發的調試工具和最佳實踐,重點關注Hbuilderx,微信開發人員工具和Chrome DevTools等工具。

本文討論了跨多個平台的Uniapp應用程序的端到端測試。它涵蓋定義測試方案,選擇諸如Appium和Cypress之類的工具,設置環境,寫作和運行測試,分析結果以及集成

本文討論了針對Uniapp應用程序的各種測試類型,包括單元,集成,功能,UI/UX,性能,跨平台和安全測試。它還涵蓋了確保跨平台兼容性,並推薦Jes等工具

本文討論了UNIAPP開發中的共同績效抗模式,例如過度的全球數據使用和效率低下的數據綁定,並提供策略來識別和減輕這些問題,以提高應用程序性能。

本文討論了通過壓縮,響應式設計,懶惰加載,緩存和使用WebP格式來優化Uniapp中的圖像,以更好地進行Web性能。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Dreamweaver Mac版
視覺化網頁開發工具

記事本++7.3.1
好用且免費的程式碼編輯器

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Dreamweaver CS6
視覺化網頁開發工具