There is such a requirement: an import button, click the button to import an Excel table. If some data in the Excel table fails to be imported, the Excel table of the failed data will be automatically downloaded. If all data is imported successfully, it will prompt "Import successful". First, attach the upload file component of ElementUI
The official website introduces the properties and usage of the 1. Introduce ElementUI upload component upload<el-upload class="upload-demo" action="https://jsonplaceholder.typicode.com/posts/" multiple :auto-upload="false" :file-list="fileList" :on-change="fileChange"> <el-button type="primary">Import</el-button> </el-upload> Page Effects Attribute Introduction
2. Click the Import button and select the file (click "Open" to trigger on-change) At this point, you can use the fileChange(file,fileList){ console.log(file,'file') console.log(fileList,'fileList') } 3. Now we have the selected file and can customize the upload method to send it to the backend serverfileChange(file,fileList){ console.log(file,'file') console.log(fileList,'fileList') let url = 'xxx' //Backend server API let headers = { 'Content-Type':'multipart/form-data' //When custom uploading, this request header parameter is required} let formData = '' for(let i = 0;i < fileList.length;i++){ //Traverse the file array, there may be multiple files in fileList formData = new FormData() formData.append('name',fileList[i].name) formData.append('type','.xlsx') formData.append('file',fileList[i].raw) } this.$axios({ headers: headers, method: 'post', data: formData, url: url, responseType:'blob' //This parameter is required, otherwise the downloaded Excel table will prompt that the file is damaged and cannot be opened}).then(res=>{ if(res && res.data.size == 0){ //If the background does not return a stream, it means that all data has been imported successfully, and a prompt "Import successful" will be displayed. Return will not be automatically downloaded. } //If the background returns a stream, it means that some data import failed, then the Excel table of the failed data will be automatically downloaded let name = 'Import failed data.xlsx' //Customize the download Excel table name let blob = new Blob([res.data]) let url = window.URL.createObjectURL(blob) let aLink = document.createElement('a') aLink.style.display = 'none' aLink.href = url //The download attribute defines the address of the download link. The href attribute must be specified in the <a> tag. aLink.setAttribute('download',name) document.body.appendChild(aLink) aLink.click() document.body.removeChild(aLink) window.URL.revokeObjectURL(url) //Other operations can be performed after the download is completed, such as refreshing the list, friendly prompts, etc.}) } Method Analysis A Summary: The above implements custom import of Excel tables and automatically downloads the stream returned by the interface. The code can be used directly, but please note that the returned data The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: The 6 Most Effective Ways to Write HTML and CSS
This article example shares the specific code of ...
In order to efficiently meet requirements and avo...
When using Nginx as a Web server, I encountered t...
1. Download Dependency npm install @antv/data-set...
Specific method: (Recommended tutorial: MySQL dat...
01. VMware Workstation Pro 15 Download Download: ...
1. Introduction In the past, if you wanted to emp...
Table of contents 1. for loop 2. Double for loop ...
This article shares the specific code of javascri...
Table of contents One-way data flow explanation V...
This article example shares the specific code of ...
<br /> The website access speed can directly...
[LeetCode] 182.Duplicate Emails Write a SQL query...
Table of contents 1. Example: this can directly g...
Today we will look at why master-slave delay occu...