elementUi上传图片

element上传图片

 <div>
     <!-- :headers='{token:token}' -->
      <el-upload class="upload-demo fl" 
                :action="uploadUrl" accept="jpg jpeg、png、gif" 
                :before-upload="beforeUpload" :on-success="uploadSuccess" :on-change="change" :on-remove="remove"
                :name="name"
                :file-list="fileList" :limit="1">
        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
      </el-upload>
  </div>

js

<script>
export default {
  data() {
    return {
      uploadUrl: "ulr/img",
      fileList: [],
      uploadFile:'',
      name:'multipartFile'
    };
  },
  methods: {
   

    // 上传成功
    uploadSuccess(response, file, fileList) {
      //上传图片成功的回调
      console.log(response);
      let arr = [];
      // fileList.forEach(item => {
      //   let testmsg = file.name.substring(file.name.lastIndexOf(".") + 1);
      //   arr.push(item.response.url);
      // });
      this.uploadFile = arr.join(",");
      this.fileList = fileList;
      // if (fileList.length == 3) {
      //   document.getElementsByClassName("el-upload")[0].style.display = "none";
      // }
    },
    change(files, fileList) {
      //修改图片列表的回调
    },
    beforeUpload(file) {
      for (let i = 0; i < this.fileList.length; i++) {
        if (file.name == this.fileList[i].name) {
          this.$message.error("不能上传相同的文件!");
          return false;
        }
      }
      var testmsg = file.name.substring(file.name.lastIndexOf(".") + 1);
      const isJPG = testmsg === "jpg";
      const isjpeg = testmsg === "jpeg";
      const ispng = testmsg === "png";
      const isgif = testmsg === "gif";
      const iszip = testmsg === "zip";
      const isLt5M = file.size / 1024 / 1024 < 5;
      const isLt20M = file.size / 1024 / 1024 < 20;
      if (!isJPG && !isjpeg && !ispng && !isgif && !iszip) {
        this.$message.error("上传未见只能是 jpg、jpeg、png、gif,zip 格式!");
        return false;
      }
      // if (isJPG && !isLt5M) {
      //   this.$message.error("上传图片jpg大小不能超过 5MB!");
      // }
      // if (isjpeg && !isLt5M) {
      //   this.$message.error("上传图片jpeg大小不能超过 5MB!");
      // }
      // if (ispng && !isLt5M) {
      //   this.$message.error("上传图片png大小不能超过 5MB!");
      // }
      // if (isgif && !isLt5M) {
      //   this.$message.error("上传图片gif大小不能超过 5MB!");
      // }
      // if (iszip && !isLt20M) {
      //   this.$message.error("上传压缩文件大小不能超过 20MB!");
      // }
    },
    remove(file, fileList) {
      //删除图片的回调
      let arr = [];
      // fileList.forEach(item => {
      //   arr.push(item.response.url);
      // });
      this.fileList = fileList;
      this.uploadFile = arr.join(",");

      // if (fileList.length == 3) {
      //   document.getElementsByClassName("el-upload")[0].style.display = "none";
      // } else {
      //   document.getElementsByClassName("el-upload")[0].style.display =
      //     "inline-block";
      // }
    }
  }
};
</script>

不建议参考 不是那么完善

作为AI语言模型,我无法进行代码编写和图形界面操作。但我可以为您提供ElementUI上传图片的基本步骤: 1. 引入ElementUI组件库和相关样式文件。 ``` import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 2. 在HTML模板中添加上传组件。 ``` <el-upload class="upload-demo" action="/upload" :on-success="handleSuccess" :before-upload="beforeUpload" :on-error="handleError" :file-list="fileList" :auto-upload="false" :multiple="true"> <el-button slot="trigger" size="small" type="primary">选取文件</el-button> <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> ``` 3. 在Vue组件中定义上传相关的方法。 ``` export default { data() { return { fileList: [] } }, methods: { beforeUpload(file) { const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' const isLt2M = file.size / 1024 / 1024 < 2 if (!isJPG) { this.$message.error('上传图片只能是 JPG/PNG 格式!') return false } if (!isLt2M) { this.$message.error('上传图片大小不能超过 2MB!') return false } return true }, handleSuccess(response, file, fileList) { this.$message.success('上传成功') }, handleError(error, file, fileList) { this.$message.error('上传失败') }, submitUpload() { this.$refs.upload.submit() } } } ``` 以上是ElementUI上传图片的基本步骤,您可以根据具体的业务需求进行相应的调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值