vue can operate local files. The operation method is: 1. Use "XMLHttpRequest" to read local files; 2. Use the "input" tag to upload files, and then use the "FileReader" object and asynchronous api , read the data in the file.
#The operating environment of this tutorial: Windows 10 system, Vue version 3, Dell G3 computer.
Can vue operate local files?
Can.
The Vue project displays the content by reading the local file content
Company projects need to implement customer-defined project titles before logging in. Since you haven't logged in yet, you definitely can't configure it through the back-end management system.
The first way that comes to mind is to display the title content by reading the content of the local file.
When customers need to change the title, they can directly modify the content of the local file.
to read local files. The first is to use XMLHttpRequest, and the second is to use input type=file to The file is uploaded first and then read.
Use XMLHttpRequest to read local files. It is worth noting that the format of the HTML document must be consistent with the reading format setting in the stream. Consistent, the code is as follows:
methods: { readFile(filePath) { // 创建一个新的xhr对象 let xhr = null if (window.XMLHttpRequest) { xhr = new XMLHttpRequest() } else { // eslint-disable-next-line xhr = new ActiveXObject('Microsoft.XMLHTTP') } const okStatus = document.location.protocol === 'file' ? 0 : 200 xhr.open('GET', filePath, false) xhr.overrideMimeType('text/html;charset=utf-8') xhr.send(null) return xhr.status === okStatus ? xhr.responseText : null },}
First create a function readFile that reads file content. Through the XMLHttpRequest object, read the file in the specified path, specify the format as text/html, and return the content.
Then directly call and read the file content in the created hook function of the login component, and assign it to the title attribute that needs to be displayed.
created() { this.getList() this.title = this.readFile('../../../static/title.txt') console.log(this.title) },
The requirements of this project are solved using this solution.
Upload the file using the input tag, and then use the FileReader object and the asynchronous api provided by h5 to read the data in the file.
Recommended learning: " vue video tutorial"The above is the detailed content of Can vue operate local files?. For more information, please follow other related articles on the PHP Chinese website!