Vue+Axios實(shí)現(xiàn)文件上傳自定義進(jìn)度條
前文: 之前一直用Elemet-UI的upload組件,但是ui給出的樣式Element-UI滿足不了,所以決定自己寫一個(gè)玩玩
總體分三步:
1、頁面布局(自定義上傳組件樣式)
2、Axios上傳
3、監(jiān)聽Process 聯(lián)動頁面實(shí)現(xiàn)進(jìn)度條
成果
1、頁面布局
<div class="display-upload-wrapper">? ? <div class="innier-upload-wrapper" :style="innerUploadStyle">? ? ? ?自定義的upload樣式? ? ? ?<div v-if="fileInfo">{{ fileInfo.name }}.{{ fileInfo.format }} 上傳完成</div>? ? ?</div>? </div>? <input id="upload-file" ref="uploadInput" type="file" @change="getFile">
通過input file 上傳文件 ,原生的upload input 太丑了,好多人是不是都忘接了什么樣子了,我?guī)痛蠹一貞浺幌?/p>
我們可以通過css隱藏這個(gè)文件,讓后用js 給其他的dom綁定上這個(gè)input的點(diǎn)擊事件實(shí)現(xiàn)
CSS
.display-upload-wrapper { ? border: 1px solid red; ? width: 384px; ? height: 54px; ? cursor: pointer; ? width: 244px; ? border-radius: 4px; ? background: #F4F8FF; ? .innier-upload-wrapper { ? ? height: 100%; ? ? background: linear-gradient(270deg, #C0D8FF 0%, #E7F2FF 100%); ? ? background-repeat: no-repeat; ? ? background-size: 10% 100%; ? ? transition: background-size .3s linear; ? } } #upload-file { ? display: none; }
js
document.querySelector('.display-upload-wrapper').onclick = function() { ? document.querySelector('#upload-file').click() }
這樣點(diǎn)擊就可以調(diào)起文件選擇
2、Axios上傳
獲取到選中的文件
getFile() { ? ?const file = this.$refs.uploadInput.files[0] ? ? if (!file) return ? ? // 獲取到的file用FormData處理成表單鍵值對 ? ? const formData = new FormData() ? ? formData.append('file', file) ? ?//uplaodFileApi是文件上傳的api 第一個(gè)入?yún)樯蟼鞯奈募诙€(gè)入?yún)樯蟼鞯倪M(jìn)度的回調(diào) ? ? uplaodFileApi(formData, this.onProcess).then(res => { ? ? ? console.log('uplaodFileApi succ: ', res) ? ? ? const { success, msg, data } = res ? ? ? if (success) { ? ? ? ? this.fileInfo = data ? ? ? } ? ? }) ? },
獲取到的file用FormData處理成表單鍵值對
const formData = new FormData()
formData.append('file', file)Axios的入?yún)?/p>
{ ? ? "method":"POST", ? ? "url":"/jz/boss/public/upload/b", ? ? "data":{ ? ? ? }, ? ? "params":{ ? ? ? ? "appToken":"xxxxxxxxxxxxxxxxxxxxx =" ? ? }, ? ? "withCredentials":true, ? ? "headers":{ ? ? ? ? "Content-Type":"multipart/form-data;charset=UTF-8" ? ? }, ? ? "responseType":"" }
data的值就是傳入的fromData,控制臺直接打印不出的
要注意的是 headers的Content-Type 要設(shè)置成multipart/form-data;charset=UTF-8 "
Content-Type":"multipart/form-data;charset=UTF-8"
做完這些操作我們就可以上傳成功了
3、監(jiān)聽Process 聯(lián)動頁面實(shí)現(xiàn)進(jìn)度條
Axios提供了onUploadProgress的回調(diào)
所有原生的processs的處理都可以,下面的圖就是這個(gè)回調(diào)的progressEvent
用total 和loaded我們就可以算出進(jìn)度條的百分比
onProcess(e) { ? const { loaded, total } = e ? const uploadPrecent = ((loaded / total) * 100) | 0 ? this.uploadPrecent = uploadPrecent },
完整代碼
<template> ? <div> ? ? {{ uploadPrecent }}% ? ? <div class="display-upload-wrapper"> ? ? ? <div class="innier-upload-wrapper" :style="innerUploadStyle"> ? ? ? ? 自定義的upload樣式 ? ? ? ? <div v-if="fileInfo">{{ fileInfo.name }}.{{ fileInfo.format }} 上傳完成</div> ? ? ? </div> ? ? </div> ? ? <input id="upload-file" ref="uploadInput" type="file" @click="clearPreUpload" @change="getFile"> ? </div> </template> ? <script> import { uplaodFileApi } from '@/api/uploadApi' import { UploadStatus } from './format' export default { ? name: 'Myupload', ? ? data() { ? ? return { ? ? ? uplaodStatus: UploadStatus.wait, ? ? ? uploadPrecent: 0, ? ? ? timer: undefined, ? ? ? fileInfo: undefined ? ? } ? }, ? computed: { ? ? innerUploadStyle() { ? ? ? return `background-size: ${this.uploadPrecent}% 100%;` ? ? } ? }, ? mounted() { ? ? this.bindUplaodClickToDisplayUplaod() ? }, ? ? methods: { ? ? bindUplaodClickToDisplayUplaod() { ? ? ? document.querySelector('.display-upload-wrapper').onclick = function() { ? ? ? ? document.querySelector('#upload-file').click() ? ? ? } ? ? }, ? ? getFile() { ? ? ? const file = this.$refs.uploadInput.files[0] ? ? ? if (!file) return ? ? ? const formData = new FormData() ? ? ? formData.append('file', file) ? ? ? uplaodFileApi(formData, this.onProcess).then(res => { ? ? ? ? const { success, msg, data } = res ? ? ? ? if (success) { ? ? ? ? ? this.fileInfo = data ? ? ? ? } ? ? ? }) ? ? }, ? ? onProcess(e) { ? ? ? const { loaded, total } = e ? ? ? const uploadPrecent = ((loaded / total) * 100) | 0 ? ? ? this.uploadPrecent = uploadPrecent ? ? }, ? ? clearPreUpload() { ? ? ? } ? } } </script> ? <style lang="scss" scoped> ? .display-upload-wrapper { ? ? border: 1px solid red; ? ? width: 384px; ? ? height: 54px; ? ? cursor: pointer; ? ? width: 244px; ? ? border-radius: 4px; ? ? background: #F4F8FF; ? ? .innier-upload-wrapper { ? ? ? height: 100%; ? ? ? background: linear-gradient(270deg, #C0D8FF 0%, #E7F2FF 100%); ? ? ? background-repeat: no-repeat; ? ? ? background-size: 10% 100%; ? ? ? transition: background-size .3s linear; ? ? } ? } ? #upload-file { ? ? display: none; ? } </style>
這個(gè)請求代碼刪減過 僅供參考可以理解為 偽代碼
const HttpRequest = (type, option) => { ? const options = { ? ? expirys: true, ? ? ...option ? } ? return new Promise((resolve, reject) => { ? ? const queryParams = ? ? ? { ? ? ? ? ? method: type, ? ? ? ? ? url: options.url, ? ? ? ? ? data: options.data, ? ? ? ? ? params: { appToken: requestToken() }, ? ? ? ? ? withCredentials: true, ? ? ? ? ? headers: options.header ? options.header : DEFAULT_HEADER, ? ? ? ? ? responseType: options.responseType || '' ? ? ? ? } ? ? // 如果有onProcess就給axios綁定onUploadProgress回調(diào) ? ? if (options.onProcess) { ? ? ? queryParams.onUploadProgress = options.onProcess ? ? } ? ? if (options.timeout) { ? ? ? queryParams.timeout = options.timeout ? ? } ? ? axios(queryParams) ? ? ? .then( ? ? ? ? res => { ? ? ? ? ? const { data = {}, headers = {} } = res || {} ? ? ? ? ? const result = Object.assign(data, headers) ? ? ? ? ? resolve(result) ? ? ? ? }, ? ? ? ? err => { ? ? ? ? ? reject(err) ? ? ? ? } ? ? ? ) ? ? ? .catch(error => { ? ? ? ? reject(error) ? ? ? }) ? ? ? .finally(() => {}) ? }) }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue+axios實(shí)現(xiàn)文件上傳的實(shí)時(shí)進(jìn)度條
- axios進(jìn)度條onDownloadProgress函數(shù)total參數(shù)undefined解決分析
- vue+element-ui+axios多文件上傳的實(shí)現(xiàn)并顯示整體進(jìn)度
- axios實(shí)現(xiàn)文件上傳并獲取進(jìn)度
- axios+Vue實(shí)現(xiàn)上傳文件顯示進(jìn)度功能
- 使用axios實(shí)現(xiàn)上傳圖片進(jìn)度條功能
- vue使用axios實(shí)現(xiàn)文件上傳進(jìn)度的實(shí)時(shí)更新詳解
- javascript axios 實(shí)現(xiàn)進(jìn)度監(jiān)控的示例代碼
相關(guān)文章
Vue學(xué)習(xí)筆記進(jìn)階篇之過渡狀態(tài)詳解
本篇文章主要介紹了Vue學(xué)習(xí)筆記進(jìn)階篇之過渡狀態(tài)詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07vue項(xiàng)目報(bào)錯:Missing?script:"serve"的解決辦法
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目報(bào)錯:Missing?script:"serve"的解決辦法,"missing script: serve"是一個(gè)錯誤信息,意味著在執(zhí)行啟動腳本時(shí),找不到名為"serve"的腳本,需要的朋友可以參考下2023-11-11vue實(shí)現(xiàn)點(diǎn)擊按鈕切換背景顏色的示例代碼
這篇文章主要介紹了用vue簡單的實(shí)現(xiàn)點(diǎn)擊按鈕切換背景顏色,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06vue數(shù)據(jù)更新但視圖(DOM)不刷新的幾種解決辦法
這篇文章主要給大家介紹了關(guān)于vue數(shù)據(jù)更新但視圖(DOM)不刷新的幾種解決辦法,我們在開發(fā)過程中經(jīng)常會碰到數(shù)據(jù)更新,但是視圖并未改變的情況,需要的朋友可以參考下2023-08-08