el-upload上传文件服务器,vue+elementui 使用el-upload组件实现单个文件手动上传

本文介绍如何使用Vue.js实现文件上传功能,包括before-upload处理、文件移除和上传状态管理。重点讲解了如何在用户选择文件后提交到服务器,并处理上传成功和失败的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ref='upload'

style="width: 100%"

:action="uploadUrl"

:on-remove="handleRemove"

:on-change="handleChange"

:data="uploadData"

:file-list="filelist"

:before-upload="handleBeforeUpload"

:auto-upload="false">

选取文件

仅支持上传单个文件

上传到服务器

export default {

data() {

return {

uploadUrl: '',

uploadData: {},

filelist: [],

filelist_temp: [], //仅为判断是否已经上传文件使用

}

},

methods: {

// 附件上传前处理

handleBeforeUpload(file,filelist) {

//处理其他数据

this.uploadData.create_user = ''

this.uploadData.type = ''

},

// 附件移除

handleRemove(file,filelist) {

//this.filelist = filelist //这种方式无法触发手动上传动作

filelist.splice(0,1)

this.file_list_temp = filelist

this.$forceUpdate()

},

// 附件变更、上传成功,上传失败

handleChange(file,filelist) {

if(file.status === 'ready') {

// 附件变更

//this.filelist = filelist //这种方式无法触发手动上传动作

if(filelist.length > 1) {

filelist.splice(0,1)

}

this.file_list_temp = filelist

this.$forceUpdate()

}else if(file.status === 'success') {

this.$message({

type: 'success',

message: '上传附件成功!'

})

}else {

this.$message({

type: 'error',

message: '上传附件失败,请重试!'

})

}

},

// 上传到服务器

submitUpload() {

// if(this.filelist.length > 0) //

if(this.filelist_temp.length > 0) {

this.$refs.upload.submit()

}else {

this.$message({

type: 'info',

message: '请先上传附件!'

})

}

},

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值