vue3 +axios 文件上传和下载
Vue3 + Axios 文件上传及下载
在 Vue3 中,与后端进行文件上传和下载是一个常见需求。本文将介绍如何使用 Axios 进行文件上传和下载,以及如何在 Vue3 项目中实现这些功能。
文件上传
1. 后端准备
首先,我们需要在后端准备接口来处理文件上传。这里以 Node.js 和 Express 为例,使用 multer
中间件来处理文件上传。
安装所需的依赖:
npm install multer
在后端的路由中引入 multer
并配置上传的目标路径和文件名:
const express = require('express');
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });
app.post('/upload', upload.single('file'), (req, res) => {
// 文件上传成功后的逻辑
});
2. 前端实现
在 Vue3 项目中,我们使用 Axios 来发送文件上传请求。首先,在项目目录下安装 Axios:
npm install axios
然后,在需要上传文件的组件中引入 Axios:
import axios from 'axios';
接下来,创建一个 uploadFile
方法来处理文件上传请求:
methods: {
async uploadFile() {
const file = this.$refs.fileInput.files[0];
const formData = new FormData();
formData.append('file', file);
try {
const response = await axios.post('/upload', formData, {
headers: { 'Content-Type': 'multipart/form-data' },
});
console.log(response.data);
} catch (error) {
console.error(error);
}
},
}
在 HTML 模板中,我们需要添加一个文件上传的输入框和一个触发上传的按钮:
<input ref="fileInput" type="file" accept="image/*" />
<button @click="uploadFile">上传文件</button>
这样,当用户选择文件后点击上传按钮,文件将被发送到后端进行处理。
文件下载
1. 后端准备
在后端,我们需要提供一个接口来供前端下载文件。这里以 Express 框架为例,假设我们要下载的文件在 uploads/
目录下。
在后端路由中添加下载文件的接口:
app.get('/download', (req, res) => {
const filePath = 'uploads/example.txt';
res.download(filePath);
});
2. 前端实现
在 Vue3 项目中,我们可以使用 a
标签的 download
属性来实现文件下载。首先,在需要下载文件的组件中创建一个 downloadFile
方法:
methods: {
downloadFile() {
const link = document.createElement('a');
link.href = '/download';
link.download = 'example.txt';
link.click();
},
},
在 HTML 模板中添加一个下载按钮,并调用 downloadFile
方法:
<button @click="downloadFile">下载文件</button>
这样,当用户点击下载按钮时,文件将被下载到本地。
结语
通过使用 Vue3 和 Axios,我们可以很方便地实现文件上传和下载功能。在文件上传中,我们使用 FormData 对象将文件数据发送到后端;在文件下载中,我们使用 download
属性和 a
标签来实现下载功能。这两个功能在实际开发中都是非常常见的,掌握它们可以让我们更好地完成前后端的交互。
以上就是关于 Vue3 + Axios 文件上传及下载的科普文章,希望对你有所帮助!
推荐阅读
-
OpenWrt luci 新增上传/下载和网络摄像头功能
-
位、字节、WORD、DWORD 的区别和联系 - Unicode 和 ANSI 的区别就像输入法中 "全宽 "和 "半宽 "的区别一样。 由于不同的 ANSI 编码有不同的标准(不同的字符集),对于给定的多字节字符串,我们必须知道它使用的是哪种字符集,才能知道它包含哪些 "字符"。对于 UNICODE 字符串来说,无论环境如何,它所代表的 "字符 "内容始终是相同的。Unicode 有一个统一的标准,定义了世界上大多数字符的编码,因此拉丁文、数字、简体中文、繁体中文和日文都可以存储在一个编码中。统一码是一个统一的标准,定义了世界上大多数字符的编码。 比特(Bit)和字节(Byte)的区别:例如USB2.0 标准接口的传输速率为 480Mbps,有一些人误认为是每秒 480 兆比特,同样网络带宽为 2MB,就容易误认为是每秒 2 兆比特。其实,480Mbps 应该是 480 兆比特/秒或 480 兆字节/秒,它等于 "60 兆字节/秒";同样,2MB,应该是 256 兆字节/秒。 Bit 和 Byte 译为 "比特",都是数据计量单位,比特="位 "或 "比特"。 Byte = 字节,即 1byte = 8bits,两者的换算关系为 1:8。 Mbps = mega bits per second(兆位/秒)是速率单位,因此 2M 带宽应为 2 兆位/秒,即 2MBps。MB = 兆字节(Megabytes,兆字节)是单位量,1MB/S(兆字节/秒)= 8MBPS(兆字节/秒)。 通常所说的硬盘容量是指 40GB、80GB、100GB,其中的 B 是指 Byte 也称为 "字节"。 1 KB=1024 字节 1 MB=1024 KB=1024*1024 字节 1 GB=1024 MB=1024*1024*1024 字节 例如,以前所谓的 56KB MODEM 转换过来的 56KBps 除以 8 就是 7Kbyte,所以真正从网上下载文件存在硬盘上的速度也是每秒 7Kbyte;也就是说,用 B 表示传输速度一般指 Bit;用 B 表示容量一般指 Byte。比特、字节、WORD、DWORD 的本质。
-
Java 文件下载/上传流量限制算法
-
连接 SFTP 上传和下载文件的 java 实现
-
JSch - SFTP 的 Java 实现(文件上传和下载)
-
教你 SpringBoot 轻松集成 Minio 文件上传和下载
-
Android+Spring Boot 选择+上传+下载文件
-
JAVA 通过 kerberos 验证登录调用 HDFS-HDAOOP 文件上传和下载成功版本!
-
SpringBoot 与 MultipartFile 类相结合,实现文件上传和下载
-
使用 formData [object 对象] 上传文件和输出表单文本数据