前端代码上传文件

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>
相关推荐
Wenweno0o12 小时前
0基础Go语言Eino框架智能体实战-chatModel
开发语言·后端·golang
于慨12 小时前
Lambda 表达式、方法引用(Method Reference)语法
java·前端·servlet
石小石Orz12 小时前
油猴脚本实现生产环境加载本地qiankun子应用
前端·架构
从前慢丶12 小时前
前端交互规范(Web 端)
前端
@yanyu66612 小时前
07-引入element布局及spring boot完善后端
javascript·vue.js·spring boot
CHU72903512 小时前
便捷约玩,沉浸推理:线上剧本杀APP功能版块设计详解
前端·小程序
chenjingming66612 小时前
jmeter线程组设置以及串行和并行设置
java·开发语言·jmeter
GISer_Jing12 小时前
Page-agent MCP结构
前端·人工智能
王霸天12 小时前
💥别再抄网上的Scale缩放代码了!50行源码教你写一个永不翻车的大屏适配
前端·vue.js·数据可视化
小领航12 小时前
用 Three.js + Vue 3 打造炫酷的 3D 行政地图可视化组件
前端·github