当前位置: 首页 > news >正文

文件上传 ,显示文件列

 <Dialog:visible.sync="registerProblemPieceShow"@close="registerProblemPieceClose"title="登记问题件"width="900px"v-dialogDragappend-to-bodydestroy-on-close><div class="registerProblemPiece_content"><RegisterProblemPiece ref="RegisterProblemPieceRef"></RegisterProblemPiece></div><span slot="footer" class="dialog-footer"><el-button @click="registerProblemPieceClose()">关闭</el-button><el-button type="primary" @click="registerProblemPieceSubmit()">确定</el-button></span></Dialog>// 登记问题件registerProblemPiece () {this.registerProblemPieceShow = true},registerProblemPieceClose () {this.$refs.RegisterProblemPieceRef.resetForm()this.registerProblemPieceShow = false},async registerProblemPieceSubmit () {try {const res = await this.$refs.RegisterProblemPieceRef.handleSubmit()console.log('res:', res)this.registerProblemPieceClose()} catch (error) {console.log('error:', error)this.$message.error(error.message)}},

 

<template><div class="registerProblemPiece_content"><el-form:model="registerProblemPieceForm":rules="registerProblemPieceRules":label-width="'100px'"size="medium"ref="registerProblemPieceFormRef"><el-form-item label="问题件类型" prop="problemType"><el-select v-model="registerProblemPieceForm.problemType" placeholder="请选择问题件类型" style="width: 260px"><el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option></el-select></el-form-item><el-form-item label="备注" prop="remark"><el-inputv-model="registerProblemPieceForm.remark"placeholder="请输入备注"type="textarea":rows="4":maxlength="200"show-word-limitstyle="width: 700px"></el-input></el-form-item><div class="upload-section"><el-form-item label="附件" prop="attachment" class="upload-container"><el-uploadref="upload"class="upload-component"action="#"drag:file-list="fileList":multiple="true":auto-upload="false"accept=".pdf,.jpg,.png,.jpeg":limit="5":on-change="handleChange":on-remove="handleRemove":on-exceed="handleExceed"><i class="el-icon-upload"></i><div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div></el-upload></el-form-item><div class="upload-tips"><div><i class="el-icon-warning-outline"></i> 温馨提示:</div><div>1. 最多上传5个文件</div><div>2. 格式:只允许上传 <span style="color: red">png、jpg、pdf</span></div><div>3. 文件大小:单个文件最大5M</div></div></div></el-form></div>
</template><script>
export default {data () {return {registerProblemPieceForm: {problemType: '',remark: '',attachment: []},registerProblemPieceRules: {problemType: [{ required: true, message: '请选择问题件类型', trigger: 'change' }],remark: [{ required: true, message: '请输入备注', trigger: 'blur' }]},options: [{ value: '1', label: '问题件类型1' },{ value: '2', label: '问题件类型2' }],fileList: []}},methods: {// 文件状态改变时的钩子async handleChange (file, fileList) {// 只处理新添加的文件if (file.status !== 'ready') return// 检查文件有效性并上传if (!this.isValidFile(file)) {this.$nextTick(() => this.$refs.upload.handleRemove(file))return}await this.uploadFile(file)this.fileList = fileList},// 验证文件是否有效isValidFile (file) {if (!file || !file.name) {this.$message.error('无效的文件')return false}const fileName = file.nameconst fileExt = fileName.substring(fileName.lastIndexOf('.')).toLowerCase()const validExtensions = ['.pdf', '.jpg', '.png', '.jpeg']const isValidType = validExtensions.includes(fileExt)const isValidSize = file.size / 1024 / 1024 < 5if (!isValidType) {this.$message.error(`文件 ${fileName} 格式不支持,只能上传 pdf、jpg、png、jpeg 格式!`)return false}if (!isValidSize) {this.$message.error(`文件 ${fileName} 过大,文件大小不能超过 5MB!`)return false}return true},// 处理文件上传async uploadFile (file) {try {const formData = new FormData()formData.append('file', file.raw)const res = await this.$request({url: `/file2`,method: 'post',data: formData})// 添加新上传的文件到 attachment 数组this.registerProblemPieceForm.attachment.push({...res.data,uid: file.uid || Date.now()})} catch (error) {console.error('文件上传失败:', error)this.$message.error('文件上传失败,请重试')this.$nextTick(() => this.$refs.upload.handleRemove(file))}},handleRemove (file) {this.registerProblemPieceForm.attachment = this.registerProblemPieceForm.attachment.filter((item) => item.uid !== file.uid)this.fileList = this.fileList.filter((item) => item.uid !== file.uid)},// 返回表单验证Promiseasync handleSubmit () {return new Promise((resolve, reject) => {this.$refs.registerProblemPieceFormRef.validate((valid) => {if (!valid) reject(new Error('提交失败,检查内容完整'))resolve(this.registerProblemPieceForm)})})},handleExceed () {this.$message.warning('只能上传5个文件')},// 重置表单resetForm () {this.$refs.registerProblemPieceFormRef.resetFields()this.registerProblemPieceForm = {problemType: '',remark: '',attachment: []}this.fileList = []this.$refs.upload.clearFiles()this.$refs.upload.handleRemove()},// 获取表单数据getFormData () {return this.registerProblemPieceForm}}
}
</script>
<style lang="scss" scoped>
.registerProblemPiece_content {padding-top: 30px;
}
.upload-section {display: flex;width: 100%;margin-top: 10px;.upload-container {width: 350px;margin-right: 20px;.upload-component {width: 100%;}}.upload-tips {color: #606266;font-size: 13px;line-height: 1.5;padding-top: 16px;i {color: #e6a23c;margin-right: 4px;}}
}
::v-deep .el-upload-dragger {width: 100%;height: 100px !important;line-height: 0;
}::v-deep .el-icon-upload {margin: 0;margin-bottom: 4px;line-height: normal;
}::v-deep .el-upload-list__item {transition: none !important;
}
</style>

http://www.dtcms.com/a/285786.html

相关文章:

  • 区别下IO多路复用,reactor,事件循环(EventLoop),Epoll这几个的概念以及之间的关系
  • uni-app 跳转外部连接
  • JS获取 CSS 中定义var变量值
  • Android性能优化之网络优化
  • LangChain 源码剖析(三):连接提示词与大语言模型的核心纽带——LLMChain
  • Jmeter使用教程
  • 闭包的定义和应用场景
  • [安洵杯 2019]easy_web
  • 深度学习×第10卷:她用一块小滤镜,在图像中找到你
  • DOM 文档对象模型
  • 【移动端知识】移动端多 WebView 互访方案:Android、iOS 与鸿蒙实现
  • Esbuild-新一代极速前端构建打包工具
  • 基于单片机多功能称重电子称设计
  • 前端下载文件并按GBK编码解析内容
  • C#`Array`进阶
  • Java全栈面试实录:从Spring Boot到AI大模型的深度解析
  • 现代R语言机器学习:Tidymodel/Tidyverse语法+回归/树模型/集成学习/SVM/深度学习/降维/聚类分类与科研绘图可视化
  • 135. Java 泛型 - 无界通配符
  • 【PTA数据结构 | C语言版】二叉堆的朴素建堆操作
  • 防爆手机是什么?能用普通手机改装吗?
  • 国产替代:ASP4644在电信通信设备中的测试与应用前景
  • 上网行为管理-web认证服务
  • Kotlin封装
  • JVM常用运行时参数说明
  • 机器人行业10年巨变从协作机器人到具身智能的萌芽、突破和成长——从 Automatic慕尼黑10 年看协作机器人到具身智能的发展
  • 基于单片机汽车驾驶防瞌睡防疲劳报警器自动熄火设计
  • Git--本地仓库的学习
  • 深入解析Linux系统启动全流程
  • 【Leecode 随笔】
  • 系统分析师-计算机系统-指令系统多处理机系统