欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue+Axios實(shí)現(xiàn)文件上傳自定義進(jìn)度條

 更新時(shí)間:2022年08月15日 09:54:13   作者:麥兜小心點(diǎn)  
這篇文章主要為大家詳細(xì)介紹了Vue+Axios實(shí)現(xiàn)文件上傳自定義進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前文: 之前一直用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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue--Router動態(tài)路由的用法示例詳解

    Vue--Router動態(tài)路由的用法示例詳解

    這篇文章主要介紹了Vue--Router動態(tài)路由的用法,很多時(shí)候,我們需要將給定匹配模式的路由映射到同一個(gè)組件,在?Vue?Router?中,我們可以在路徑中使用一個(gè)動態(tài)字段來實(shí)現(xiàn),我們稱之為路徑參數(shù),本文對Vue?Router動態(tài)路由相關(guān)知識給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-08-08
  • Vue3使用mitt進(jìn)行組件通信的步驟

    Vue3使用mitt進(jìn)行組件通信的步驟

    這篇文章主要介紹了Vue3使用mitt進(jìn)行組件通信的步驟,幫助大家更好的理解和學(xué)習(xí)使用vue,感興趣的朋友可以了解下
    2021-05-05
  • vite如何構(gòu)建vue3項(xiàng)目

    vite如何構(gòu)建vue3項(xiàng)目

    本文介紹了如何使用Vite快速搭建Vue項(xiàng)目,強(qiáng)調(diào)Vite對Node.js版本有最低要求(>=12.0.0),提供了環(huán)境準(zhǔn)備、安裝步驟和啟動指南,旨在幫助開發(fā)者高效啟動Vue項(xiàng)目
    2024-10-10
  • Vue學(xué)習(xí)筆記進(jìn)階篇之過渡狀態(tài)詳解

    Vue學(xué)習(xí)筆記進(jìn)階篇之過渡狀態(tài)詳解

    本篇文章主要介紹了Vue學(xué)習(xí)筆記進(jìn)階篇之過渡狀態(tài)詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • vue項(xiàng)目報(bào)錯:Missing?script:"serve"的解決辦法

    vue項(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-11
  • vue中使用swiper失效問題及解決

    vue中使用swiper失效問題及解決

    這篇文章主要介紹了vue中使用swiper失效問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 深入聊一聊虛擬DOM與diff算法

    深入聊一聊虛擬DOM與diff算法

    為什么vue等的這些mvvm框架比傳統(tǒng)的操作dom渲染頁面要快,下面這篇文章主要給大家介紹了關(guān)于虛擬DOM與diff算法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • vue中的事件綁定舉例詳解

    vue中的事件綁定舉例詳解

    這篇文章主要給大家介紹了關(guān)于vue中事件綁定的相關(guān)資料,事件綁定在Web開發(fā)中非常常見,我們經(jīng)常需要在頁面中為某個(gè)DOM元素綁定事件,如點(diǎn)擊、鼠標(biāo)移動、鍵盤敲擊等等,需要的朋友可以參考下
    2023-09-09
  • vue實(shí)現(xiàn)點(diǎn)擊按鈕切換背景顏色的示例代碼

    vue實(shí)現(xiàn)點(diǎn)擊按鈕切換背景顏色的示例代碼

    這篇文章主要介紹了用vue簡單的實(shí)現(xiàn)點(diǎn)擊按鈕切換背景顏色,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • vue數(shù)據(jù)更新但視圖(DOM)不刷新的幾種解決辦法

    vue數(shù)據(jù)更新但視圖(DOM)不刷新的幾種解決辦法

    這篇文章主要給大家介紹了關(guān)于vue數(shù)據(jù)更新但視圖(DOM)不刷新的幾種解決辦法,我們在開發(fā)過程中經(jīng)常會碰到數(shù)據(jù)更新,但是視圖并未改變的情況,需要的朋友可以參考下
    2023-08-08

最新評論