elementUI使用el-upload组件上传图片

复制代码
<el-form-item label="上传图片" :rules="[{ required: true, message: '必须要上传图片', trigger: 'blur' }]"
                        prop="image">
            <el-upload
                :action=webSite
                class="upload-demo"
                drag
                :limit="1"
                :before-upload="beforeUpload"
                :on-success="handleSuccess"
                :on-remove = "handleRemove"
                :show-file-list="true" >
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
              <div class="el-upload__tip" slot="tip">只能上传jpg/png文件,且不超过4M</div>
            </el-upload>
          </el-form-item>
复制代码

以上是页面代码

以下是逻辑代码,主要是对图片的校验,成功后的赋值,需要后台配合返回 保存的图片信息

{'file_name': file_data.name, 'url': file_path}
复制代码
    beforeUpload(file){
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt5M = file.size / 1024 / 1024 < 5

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG/PNG 格式!')
      }
      if (!isLt5M) {
        this.$message.error('上传图片大小不能超过 5MB!')
      }
      return isJPG && isLt5M
    },
    handleSuccess(response) {
      this.$message.success('上传成功!')
      this.form.image = response.url;
    },
    handleRemove(){
   this.$refs.upload.clearFiles()
 },
复制代码

 

posted @   lytcreate  阅读(391)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示