【vue】element ui - el-upload 上传校验文件,格式和大小

el-upload 上传校验文件,格式和大小

下面例子的情况是一个上传按钮,点击直接弹出选择文件

<el-upload
    ref="upload"
    class="zls-upload-demo"
    :action="actionurl"
    :on-change="handleChange"
    :show-file-list="false"
    :file-list="fileListUpload"
    :on-success="uploadSuccess"
    :before-upload="beforeFileUpload"
    accept=".pdf,.docx,.doc,.xls,.xlsx,.ceb"
  >
    <el-button
      type="primary"
      v-loading.fullscreen.lock="fullscreenLoading"
      >文档上传</el-button
    >
  </el-upload>

<el-upload>属性解释
actionurl 上传接口地址
on-change 选择文件,上传文件成功或上传文件失败时调用
show-file-list=“false” 隐藏文件列表
on-success 上传成功后调用
before-upload 上传前调用
accept接收文件格式,在选择上传文件时进行限制,如图:
请添加图片描述

v-loading.fullscreen.lock=“fullscreenLoading” 全屏loading

相关方法

beforeFileUpload(file) {
      this.fullscreenLoading = true;
      //校验文件大小
      if (file.size >= 10485760) {
        this.$message({
          type: "error",
          message: "文件大小超出10M,请重新上传!",
        });
        this.fullscreenLoading = false;
        return;
      }
	//校验文件格式
      if (
		//excel
        file.type !==
          "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" &&
		//excel
        file.type !== "application/vnd.ms-excel" &&
		//word
        file.type !== "application/msword" &&
		//pdf
        file.type !== "application/pdf" &&
		//word
        file.type !==
          "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
      ) {
        this.$message({
          type: "warning",
          message: "附件格式错误,请删除后重新上传!",
        });
        return;
      }
    },

handleChange(file, fileList) {
      this.fileTemp = file.raw;
      if (!this.fileTemp) {
        this.$message({
          type: "warning",
          message: "请上传附件!",
        });
      }
    },
    handleRemove(file, fileList) {
      this.fileTemp = null;
    },
    uploadSuccess(res, file) {
      setTimeout(() => {
        this.fullscreenLoading = false;
      }, 1000);
      this.fileTemp = file.raw;
      //获取文件大小
      let docSize = this.fileTemp.size;
      //获取文件名字
      let docName = res.data.name.substring(0, res.data.name.lastIndexOf("."));
      //获取文件格式
      let docFormat = res.data.name.substring(
        res.data.name.lastIndexOf(".") + 1
      );
      //获取文件路径
      let docPath = res.data.url;
      //成功后跳转
      this.$router.push({
        name: this.targetpage,
        params: {
          docName: docName,
          docPath: docPath,
          docFormat: docFormat,
          docSize: docSize,
        },
      });
    },
在使用vueelement-ui制作上传文件的效果时,可以按照以下步骤进行: 1. 安装element-ui:在终端中使用npm或yarn进行安装,例如:npm install element-ui --save。 2. 在vue组件中引入element-ui上传组件:在需要使用上传功能的组件中,使用import引入element-ui上传组件,例如: ``` <template> <div> <el-upload action="https://jsonplaceholder.typicode.com/posts/" :on-preview="handlePreview" :on-remove="handleRemove" :before-upload="beforeUpload" :file-list="fileList" multiple list-type="picture" :auto-upload="false" :on-success="handleUploadSuccess" :on-error="handleUploadError"> <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> </div> </template> <script> import { Upload, Button } from 'element-ui'; export default { components: { 'el-upload': Upload, 'el-button': Button }, data() { return { fileList: [] }; }, methods: { handlePreview(file) { console.log('preview', file); }, handleRemove(file) { console.log('remove', file); }, beforeUpload(file) { const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'; const isLt500k = file.size / 1024 < 500; if (!isJPG) { this.$message.error('只能上传jpg/png文件'); } if (!isLt500k) { this.$message.error('文件大小不能超过500kb'); } return isJPG && isLt500k; }, handleUploadSuccess(response, file, fileList) { console.log('success', response, file, fileList); }, handleUploadError(error, file, fileList) { console.log('error', error, file, fileList); } } }; </script> ``` 3. 配置上传组件的属性和方法:在引入上传组件后,需要根据实际需求进行配置,例如:设置上传的地址、文件大小限制、文件类型限制、上传前的校验、上传成功和失败的回调函数等。 4. 在模板中使用上传组件:在组件的模板中使用上传组件,并将需要上传的文件绑定到fileList属性上,例如:`:file-list="fileList"`。 5. 样式美化:根据实际需求对上传组件的样式进行美化,例如:设置上传按钮的颜色、设置文件列表的样式等。 通过以上步骤,就可以在vue项目中使用element-ui制作上传文件的效果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值