前端代码上传文件

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>
相关推荐
ytttr8731 分钟前
Qt 数字键盘实现
开发语言·qt
wearegogog1234 分钟前
C# .NET 文件比较工具 WinForms
开发语言·c#·.net
再写一行代码就下班8 分钟前
Cursor配置Java环境、创建Spring Boot项目的步骤
java·开发语言·spring boot
零陵上将军_xdr11 分钟前
后端转全栈学习-Day5-JavaScript 基础-3
开发语言·javascript·学习
ssshooter13 分钟前
为什么父元素的高度不会包含子元素的 margin?
前端·javascript·面试
静Yu19 分钟前
从一个九宫格素材小程序,看轻量工具产品该如何优化体验
前端·微信小程序
oqX0Cazj221 分钟前
2026超火Go-Zero实战:从架构原理到高并发接口落地,彻底解决接口超时、雪崩问题
开发语言·架构·golang
Goodbye21 分钟前
JavaScript 同步与异步编程深度解析
javascript
Amo Xiang23 分钟前
JS 逆向系统进阶路线:专栏总纲与文章导航
javascript·js逆向·前端加密·爬虫逆向·反爬虫
学会去珍惜25 分钟前
C语言简介
c语言·开发语言