element 上传图片
1. element 的上传图片
<template> <el-upload ref="imgUpload" :before-upload="(file)=>beforeAvatarSuccess(file,'xslx')" :http-request="onRequest" action="" list-type="picture-card"> <i slot="default" class="el-icon-plus"></i> <div slot="file" slot-scope="{file}"> <img :src="file.url" alt="" class="el-upload-list__item-thumbnail" > <span class="el-upload-list__item-actions"> <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)" > <i class="el-icon-zoom-in"></i> </span> <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleDownload(file)" > <i class="el-icon-download"></i> </span> <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file,'imgUpload')" > <i class="el-icon-delete"></i> </span> </span> </div> </el-upload> <el-dialog :visible.sync="dialogVisible"> <img :src="dialogImageUrl" alt="" width="100%"> </el-dialog> </template> <script> export default { name: 'About', data() { return { isImages: '', dialogImageUrl: '', dialogVisible: false, imageList: { xslx: [] } } }, methods: { beforeAvatarSuccess(file, id) { const isJPG = file.type === 'image/jpg' || file.type === 'image/jpeg' || file.type === 'image/png'; const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG) { this.$message.error('只能上传png,jpg,jpeg类型的图片') } if (!isLt2M) { this.$message.error('上传图片不能超过2M') } this.isImages = id return isLt2M && isJPG }, //上传图片 onRequest(params) { let formdata = new FormData() formdata.append('file', params.file) uploadImgs(formdata).then(res => { if (res.status) { this.$message.success('上传成功') this.imagesList[this.isImages].push(res.data) } }) }, handlePictureCardPreview(file) { this.dialogImageUrl = file.url this.dialogVisible = true }, //删除图片 handleRemove(file, ref) { let uploadFiles = this.$refs[ref].uploadFiles; for (var i = 0; i < uploadFiles.length; i++) { if (uploadFiles[i]['url'] == file.url) { uploadFiles.splice(i, 1) } } }, handleDownload(file) { window.open(file,url) } } } </script>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY