Can vue operate local files?

藏色散人
Release: 2023-01-05 15:45:33
Original
3078 people have browsed it

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.

Can vue operate local files?

#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

Requirements:

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.


There are two ways to implement

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.

First type:

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 },}
Copy after login

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) },
Copy after login

The requirements of this project are solved using this solution.


Second type:

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.

     Document 
点击上传:
点击上传2:
Copy after login
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!

Related labels:
vue
source:php.cn
Statement of this Website
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