"; 3. Use the logic code "methods: {beforeUpload(file) {...}" to determine the file size."/> "; 3. Use the logic code "methods: {beforeUpload(file) {...}" to determine the file size.">

Home  >  Article  >  Web Front-end  >  How to realize that vue file upload cannot be larger than 10mb

How to realize that vue file upload cannot be larger than 10mb

藏色散人
藏色散人Original
2023-01-29 14:30:582369browse

Vue file upload cannot be larger than 10mb. Implementation method: 1. Create "index.vue"; 2. Set the structure to ""; 3. Use the logic code "methods: {beforeUpload(file) {...}" to determine the file size.

How to realize that vue file upload cannot be larger than 10mb

#The operating environment of this tutorial: Windows 10 system, vue3 version, DELL G3 computer

How to realize that vue file upload cannot be larger than 10mb?

In vue, the Upload upload component el-upload uses -zip format, the size does not exceed 10M & obtain and save the token in the store

Effect

How to realize that vue file upload cannot be larger than 10mb

Code

index.vue

Structure
 <el-form>
    <el-form-item>
        <el-upload>
            <el-button>上传</el-button>
            zip格式,大小不超过10M
        </el-upload>
    </el-form-item>
    <el-form-item>
        <el-button>立即添加</el-button>
        <el-button>取消</el-button>
    </el-form-item>
</el-form>
Logic
<script>import store from &#39;@/store/index&#39;;export default {
  data() {
    return {
      upload: {
        url: `${App.apiConfig.service}/dcp-manage/dcp/compile/manage/service/editor/version/upload`,
        header: {
          token: store.state.token        },
        resData: {}
      },
      form: {
        nameDesc: &#39;&#39;,
        age:&#39;&#39;
      },
      rules: {
        nameDesc: [
          { required: true, message: &#39;不能为空&#39;, trigger: &#39;blur&#39; }
        ],          
        fileName: [
          { required: true, message: &#39;不能为空&#39;, trigger: &#39;blur&#39; }
        ]
      }        
    }
  },    
  methods: {
    beforeUpload(file) {
      const sizeLimit = file.size / 1024 / 1024 > 10
      if (sizeLimit) {
        this.$message.warning(&#39;上传文件大小不能超过 10MB!&#39;)
      }
      const fileFamart = file.name.split(&#39;.&#39;)[file.name.split(&#39;.&#39;).length - 1];
      if (fileFamart !== &#39;zip&#39;) {
        this.$message.warning(&#39;必须上传zip格式的文件!&#39;)
      }
      return !sizeLimit && fileFamart === &#39;zip&#39;
    },
    onSuccess(data) {
      this.form = {
        ...this.form,
        ...data.data  // 把上传文件添加到form中
      }
      this.$message.success(&#39;上传成功!&#39;);
    },
    onError() {
      this.$message.error(&#39;上传失败!&#39;);
    },
    // 立即添加
    onSubmit() {
      this.$refs.ruleForm.validate(valid => {  // 校验form表单
        if (!valid) return false;

        ajax.post(&#39;cs_addVersion&#39;, this.form).then(res => {
          this.$message.success(&#39;新增成功!&#39;);
          this.backToList();  // 新增成功后返回
        }, err => {
          this.$message.error(&#39;新增失败!&#39;);
        });
      });
    },
    // 取消
    backToList() {
      this.$router.back();
    }      
  }}</script>
Storage
src\store\index.js

/*
 * @Descripttion:
 * @version:
 * @Author:
 * @Date: 2022-06-04 09:34:23
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-06-04 11:20:26
 */import Vue from 'vue';import Vuex from 'vuex';Vue.use(Vuex);// 状态const state = {
  token: sessionStorage.getItem('token'),
  userInfo: JSON.parse(sessionStorage.getItem('userInfo')),}// mutations用来操作stateconst mutations = {
  // 保存token
  SAVE_TOKEN (state, token) {
    sessionStorage.setItem('token', token);
    state.token = token;
  },
  // 保存用户相关信息
  SAVE_USERINFO (state, userInfo) {
    sessionStorage.setItem('userInfo', JSON.stringify(userInfo));
    state.userInfo = userInfo;
  },}export default new Vuex.Store({
  state,
  mutations})
recommends learning: "vue video tutorial

The above is the detailed content of How to realize that vue file upload cannot be larger than 10mb. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn