前端代码上传文件

html 复制代码
<template>
  <el-upload :class=BASE_URL drag :action="API_CONFIG.uploadFile" type="file" multiple :limit="1"
    accept=".xlsx,.xls,.yaml,.yml,.execl,.json" v-model:file-list="file" :http-request="handleUploadFile">
    <el-icon class="el-icon--upload"><upload-filled /></el-icon>
    <div class="el-upload_font">
      <em>点击上传文件</em>
    </div>
  </el-upload>
</template>

<script setup lang="js">
import { ElNotification } from 'element-plus'
import { API_CONFIG } from '../config/index.js'
import { UploadFilled } from '@element-plus/icons-vue'
import { reactive } from 'vue'
import {BASE_URL} from '../config/index'
import axios from 'axios'

const form = reactive({
  name: '',
  file: '',
})

const handleUploadFile = (v) => {
  form.file = v.file
  form.name = v.name
  let formData = new FormData()
  formData.append("name", form.name)
  formData.append("file", form.file)
  axios.post(API_CONFIG.uploadFile, formData, {
    headers: { 'Content-Type': 'multipart/form-data' }
  }).then((response) => {
    if (response.data.status == 200) {
      ElNotification({
        title: 'Success',
        message: response.data.message,
        type: 'success',
      })
    } else {
      ElNotification({
        title: 'Error',
        message: '上传失败!',
        type: 'error',
      })  
    }
  })
}
</script>
相关推荐
xcLeigh11 分钟前
HTML5好看的水果蔬菜在线商城网站源码系列模板2
java·前端·html5
老田低代码11 分钟前
Dart自从引入null check后写Flutter App总有一种难受的感觉
前端·flutter
guojikun11 分钟前
使用 Wake Lock API:保持设备唤醒的最佳实践
javascript·web前端·wakelock
啊QQQQQ13 分钟前
C++11(3)
java·开发语言·c++
专家大圣15 分钟前
C#:强大编程语言的多面魅力
开发语言·c#
草上爬16 分钟前
Qt优秀开源项目之二十三:QSimpleUpdater
开发语言·qt·qsimpleupdater·自动升级
可愛小吉18 分钟前
Python 课程15-PyTorch
开发语言·人工智能·pytorch·python·机器学习
解孔明20 分钟前
Java如何扫描指定包下所有类?
java·开发语言
luckycoke33 分钟前
小程序的右侧抽屉开关动画手写效果
前端·javascript·微信小程序·uni-app