OverviewThe specific demand scenarios are as follows: After selecting the Excel file, you need to manually upload the imported Excel file to the backend server and display the statistical results after successful import. The official website also has examples of manual uploads, which pass in the address through action="url", but in actual projects, requests need to be configured by yourself. The following describes how to do it. illustrate: From uploading files to displaying statistical results, our backend provides two interfaces: first, call the file upload interface, and after the upload is successful, call the statistical result interface based on the mark returned by the backend. Property settings.vue Files <el-row> <div class="el-form-item__content"> <el-upload ref="upload" accept=".xls,.xlsx" action="#" :auto-upload="false" :multiple="false" :file-list="fileList" :before-upload="beforeUpload" :http-request="uploadHttpRequest" :on-remove="fileRemove" :on-change="fileChange"> <el-button size="small" type="primary">Select file</el-button> <div slot="tip" class="el-upload__tip">Only one xls/xlsx file can be uploaded at a time, and the file size should not exceed 10M</div> </el-upload> </div> </el-row> <el-row> <el-button size="small" @click="closeDialog">Cancel</el-button> <el-button type="primary" size="small" @click="submitUpload">Upload</el-button> <el-button type="primary" size="small" @click="downloadRes">Download feedback results</el-button> </el-row> in:
Processing LogicThe logic processing code is as follows: methods: { // Hook before uploading files: determine the format and size of the uploaded file, and stop uploading if false is returned beforeUpload(file) { //File type const isType = file.type === 'application/vnd.ms-excel' const isTypeComputer = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' const fileType = isType || isTypeComputer if(!fileType) { this.$message.error('Uploaded files can only be in xls/xlsx format!') } // The file size limit is 10M const fileLimit = file.size / 1024 / 1024 < 10; if(!fileLimit) { this.$message.error('The uploaded file size does not exceed 10M!'); } return fileType && fileLimit }, // Custom upload method, param is the default parameter, you can get the file information, the specific information is as follows uploadHttpRequest(param) { const formData = new FormData() //FormData object, adding parameters can only be done through append('key', value) formData.append('file', param.file) //Add file object formData.append('uploadType', this.rulesType) const url = `${this.myBaseURL}/operation/ruleImport/importData` //Upload address axios.post(url, formData) .then( res => { const { data: { code, mark } } = res if(code === 0) { param.onSuccess() // Successfully uploaded files display a green check mark this.uploadMark = mark } return this.countData(this.uploadMark) //Call the statistics result interface according to the response mark value and return a promise for chain call}) .then( res => { //Chain call, response to statistical results const { data: { code, data } } = res if(code === 0) { console.log('Statistical results', data) } }) .catch( err => { console.log('failed', err) param.onError() //Files that failed to upload will be deleted from the file list}) }, // Statistical results countFile(mark) { return new Promise((resolve, reject) => { axios .get(`/operation/ruleImport/countData?mark=${mark}`) .then(res => { resolve(res) }) .catch(error => { reject(error) }) }) }, // Click to upload: Manually upload to the server, which will trigger the component's http-request submitUpload() { this.$refs.upload.submit() }, // The file changes fileChange(file, fileList) { if (fileList.length > 0) { this.fileList = [fileList[fileList.length - 1]] // Display the last selected file} }, // Remove the selected files fileRemove(file, fileList) { if(fileList.length < 1) { this.uploadDisabled = true // Disable the upload button if no file is selected} }, //Cancel closeDialog() { this.$refs.upload.clearFiles() //Clear uploaded file object this.fileList = [] //Clear the selected file list this.$emit('close', false) } } The param parameter of http-request is printed as shown in the figure. Get the current file object through param.file. The above is a detailed explanation of how to manually upload Excel files to the server using Vue ElementUI. For more information about how to manually upload Excel files to the server using Vue ElementUI, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Solution to the problem that mysql local login cannot use port number to log in
>>: Summary of learning Docker commands in one article
Table of contents 1. What is vuex 2. Installation...
Download and install MySQL 8.0.22 for your refere...
For several reasons (including curiosity), I star...
Repetition: Repeat certain page design styles thr...
When learning mybatis, I encountered an error, th...
Copy code The code is as follows: wmode parameter...
1. There are many Python version management tools...
This section starts with the details of text modi...
01PARTCoreWebApi tutorial local demonstration env...
Several parts of Compose deal with environment va...
1. Enter the directory where your project war is ...
Table of contents 1. Principle of animation funct...
There are three ways to interconnect and communic...
Detailed installation tutorial of mysql-5.7.23-wi...
After the worker process is started, it will firs...