vue中element-ui使用axios上傳文件
本文實例為大家分享了vue中element-ui使用axios上傳文件的具體代碼,供大家參考,具體內(nèi)容如下
環(huán)境:vue2.5.6+axios0.18.1+element-ui2.15.1
在使用element UI的upload組件上傳文件時,遇到一些問題,網(wǎng)上的說法不盡如是,在此記錄
其實最主要的估計就是axios相關(guān)的問題,因我們平時開發(fā)的vue項目都是封裝過axios后進行api的調(diào)用,但上傳操作跟一般的api請求不同,所有總是報錯,故需要建立新的axios實例。
文件上傳類型可以在el-upload的accept中進行定義,上傳時會進行一定的過濾。
例如圖片類型:accept=".png, .jpeg"
1. 封裝上傳文件的axios實例
import Vue from "vue"; import axios from "axios"; ? const instance = axios.create({ ? baseURL: "http://localhost:8080", // 后臺地址 ? withCredentials: false, ? timeout: 10000 }); Vue.prototype.axiosUpload = instance;
2. 封裝vue組件
<template> ? <el-dialog :title="title" :visible.sync="upLoadVisible" width="30%" @close="closeDialog"> ? ? <div class="upload-box"> ? ? ? <el-upload ? ? ? ? ref="upload" ? ? ? ? drag ? ? ? ? class="upload-demo" ? ? ? ? accept=".xls,.xlsx" ? ? ? ? action="#" ? ? ? ? :limit="1" ? ? ? ? :on-preview="handlePreview" ? ? ? ? :on-remove="handleRemove" ? ? ? ? :file-list="fileList" ? ? ? ? :auto-upload="false" ? ? ? ? :before-upload="beforeUpload" ? ? ? ? :http-request="uploadHttpRequest" ? ? ? > ? ? ? ? <i class="el-icon-upload" /> ? ? ? ? <div class="el-upload__text"> ? ? ? ? ? 將文件拖到此處,或<em style="color: #409eff">點擊上傳</em> ? ? ? ? </div> ? ? ? ? ? <div slot="tip" class="el-upload__tip"> ? ? ? ? ? 只能上傳xls/xlxs文件, 文件大小不超過10M ? ? ? ? </div> ? ? ? </el-upload> ? ? ? <el-progress ? ? ? ? v-if="processVisible" ? ? ? ? :percentage="processPercent" ? ? ? ? :text-inside="true" ? ? ? ? :stroke-width="24" ? ? ? ? :status="processStatus" ? ? ? ? style="margin: 20px 0;" ? ? ? /> ? ? ? <el-alert ? ? ? ? v-if="tipVisible" ? ? ? ? :title="tipTitle" ? ? ? ? type="error" ? ? ? ? @close="tipVisible = false" ? ? ? /> ? ? ? <el-button ? ? ? ? style="margin: 10px 0;" ? ? ? ? icon="el-icon-upload2" ? ? ? ? size="small" ? ? ? ? type="primary" ? ? ? ? :loading="uploading" ? ? ? ? @click="submitUpload" ? ? ? > ? ? ? ? 上傳到服務(wù)器 ? ? ? </el-button> ? ? ? <el-button ? ? ? ? style="margin-left: 0" ? ? ? ? icon="el-icon-circle-close" ? ? ? ? size="small" ? ? ? ? @click="cancelUpload" ? ? ? > ? ? ? ? 取 消 ? ? ? </el-button> ? ? </div> ? </el-dialog> </template> ? <script> export default { ? props: { ? ? title: { ? ? ? type: String, ? ? ? default: "上傳文件" ? ? }, ? ? upLoadVisible: { ? ? ? type: Boolean, ? ? ? default: false ? ? }, ? ? uploadUrl: { // 上傳文件的api ? ? ? type: String, ? ? ? default: "" ? ? } ? }, ? data() { ? ? return { ? ? ? fileList: [], ? ? ? uploading: false, ? ? ? processPercent: 5, ? ? ? processStatus: 'success', ? ? ? processVisible: false, ? ? ? tipVisible: false, ? ? ? tipTitle: '', ? ? ? timer: null ? ? }; ? }, ? methods: { ? ? // 上傳文件之前的鉤子:判斷上傳文件格式、大小等,若返回false則停止上傳 ? ? beforeUpload(file) { ? ? ? // 文件類型 ? ? ? const isType = file.type === "application/vnd.ms-excel"; ? ? ? const isTypeComputer = ? ? ? ? file.type === ? ? ? ? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; ? ? ? const fileType = isType || isTypeComputer; ? ? ? if (!fileType) { ? ? ? ? this.$message.error("上傳文件只能是xls/xlsx格式!"); ? ? ? } ? ? ? ? // 文件大小限制為10M ? ? ? const fileLimit = file.size / 1024 / 1024 < 10; ? ? ? if (!fileLimit) { ? ? ? ? this.$message.error("上傳文件大小不超過10M!"); ? ? ? } ? ? ? return fileType && fileLimit; ? ? }, ? ? // 自定義上傳方法,param是默認參數(shù),可以取得file文件信息,具體信息如下圖 ? ? async uploadHttpRequest(param) { ? ? ? const that = this; ? ? ? this.uploading = true; // 顯示按鈕加載中 ? ? ? this.processVisible = true; // 顯示進度條 ? ? ? this.processPlus(); ? ? ? // FormData對象,添加參數(shù)只能通過append('key', value)的形式添加 ? ? ? const formData = new FormData(); ? ? ? // 添加文件對象 ? ? ? formData.append("file", param.file); ? ? ? ? const config = { ? ? ? ? headers: { ? ? ? ? ? "Content-Type": "multipart/form-data" ? ? ? ? } ? ? ? }; ? ? ? this.axiosUpload.post(this.uploadUrl, formData, config).then(res => { ? ? ? ? // 這里的res需根據(jù)后臺返回情況判斷 ? ? ? ? if (res.data.code === 200) { ? ? ? ? ? this.processPercent = 100; ? ? ? ? ? that.uploading = false; ? ? ? ? ? this.processVisible = false; ? ? ? ? ? this.timer = null; ? ? ? ? ? that.$emit("closeUpload"); ? ? ? ? ? that.$message.success(res.data.msg); ? ? ? ? } else { ? ? ? ? ? this.processPercent = 100; ? ? ? ? ? this.processStatus = "exception"; ? ? ? ? ? this.tipVisible = true; ? ? ? ? ? this.tipTitle = res.data.msg; ? ? ? ? ? this.uploading = false; ? ? ? ? } ? ? ? }).catch(reject => { ? ? ? ? console.log(reject) ? ? ? }) ? ? }, ? ? // 上傳至服務(wù)器 ? ? submitUpload() { ? ? ? this.$refs.upload.submit(); ? ? }, ? ? handleRemove(file, fileList) { ? ? ? console.log(file, fileList); ? ? }, ? ? handlePreview(file) { ? ? ? console.log(file); ? ? }, ? ? cancelUpload() { ? ? ? this.$refs.upload.clearFiles(); // 清除上傳文件對象 ? ? ? this.fileList = []; // 清空選擇的文件列表 ? ? ? this.$emit("closeUpload"); ? ? }, ? ? // 前期進度條增加 ? ? processPlus() { ? ? ? this.timer = setInterval(() => { ? ? ? ? if (this.processPercent < 90) { ? ? ? ? ? this.processPercent += 10; ? ? ? ? } ? ? ? }, 200) ? ? }, ? ? closeDialog() { ? ? ? this.$emit('closeUploadDialog'); ? ? } ? } }; </script> ? <style scoped> .upload-box { ? display: flex; ? flex-direction: column; ? justify-content: center; } .upload-demo { ? display: flex; ? flex-direction: column; ? align-items: center; } </style>
3 效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue前端重構(gòu)computed及watch組件通信等實用技巧整理
這篇文章主要為大家介紹了vue前端重構(gòu)computed及watch組件通信等實用技巧整理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05Vue如何處理Axios多次請求數(shù)據(jù)顯示問題
這篇文章主要介紹了Vue如何處理Axios多次請求數(shù)據(jù)顯示問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01vue3+高德地圖只展示指定市、區(qū)行政區(qū)域的地圖以及遮罩反向鏤空其他地區(qū)
vue大屏項目開發(fā),客戶覺得地圖上的文字標注太多了,要求地圖上只顯示省市等主要城市的標注,這篇文章主要給大家介紹了關(guān)于vue3+高德地圖只展示指定市、區(qū)行政區(qū)域的地圖以及遮罩反向鏤空其他地區(qū)的相關(guān)資料,需要的朋友可以參考下2024-02-02vue項目index.html中使用環(huán)境變量的代碼示例
在Vue3中使用環(huán)境變量的方式與Vue2基本相同,下面這篇文章主要給大家介紹了關(guān)于vue項目index.html中使用環(huán)境變量的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-02-02vue 使用axios 數(shù)據(jù)請求第三方插件的使用教程詳解
這篇文章主要介紹了vue 使用axios 數(shù)據(jù)請求第三方插件的使用 ,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07Ant Design moment對象和字符串之間的相互轉(zhuǎn)化教程
這篇文章主要介紹了Ant Design moment對象和字符串之間的相互轉(zhuǎn)化教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10