"; 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
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.
#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<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>
<script>import store from '@/store/index';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: '', age:'' }, rules: { nameDesc: [ { required: true, message: '不能为空', trigger: 'blur' } ], fileName: [ { required: true, message: '不能为空', trigger: 'blur' } ] } } }, methods: { beforeUpload(file) { const sizeLimit = file.size / 1024 / 1024 > 10 if (sizeLimit) { this.$message.warning('上传文件大小不能超过 10MB!') } const fileFamart = file.name.split('.')[file.name.split('.').length - 1]; if (fileFamart !== 'zip') { this.$message.warning('必须上传zip格式的文件!') } return !sizeLimit && fileFamart === 'zip' }, onSuccess(data) { this.form = { ...this.form, ...data.data // 把上传文件添加到form中 } this.$message.success('上传成功!'); }, onError() { this.$message.error('上传失败!'); }, // 立即添加 onSubmit() { this.$refs.ruleForm.validate(valid => { // 校验form表单 if (!valid) return false; ajax.post('cs_addVersion', this.form).then(res => { this.$message.success('新增成功!'); this.backToList(); // 新增成功后返回 }, err => { this.$message.error('新增失败!'); }); }); }, // 取消 backToList() { this.$router.back(); } }}</script>
/* * @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!