vue3中文件下载

2 篇文章 0 订阅
这篇博客介绍了在 Vue3 中如何实现文件下载,包括通过 Axios 发送请求获取 Blob 数据流并创建下载链接,以及利用 A 标签直接下载 public 目录下的静态文件。在 Vue2 中,静态文件通常存放在 static 文件夹,而在 Vue3 中,如果需要,可以自行创建 static 文件夹来存放要下载的文件。
摘要由CSDN通过智能技术生成

vue3中文件下载

可以用原生数据流下载(axios)

发个请求从后台拿数据流

/**
* 例如
*/ 
 axios({
 		method:'post',//请求方式
 		url:url,//请求地址
 		responseType:'blob'//文件流将会被转成blob
 	}).then(res => {
    const blob = new Blob([res.data]);//处理文档流
    const fileName = '帮助文档.docx';
    const down = document.createElement('a');
    down.download = fileName;
    down.style.display = 'none';//隐藏,没必要展示出来
    down.href = URL.createObjectURL(blob);
    document.body.appendChild(down);
    down.click();
    URL.revokeObjectURL(down.href); // 释放URL 对象
    document.body.removeChild(down);//下载完成移除
})

这里说一下用a标签在vue2和vue3的文件下载

在vue2中public下有文件夹static,静态文件资源存放处,需要下载的帮助文档可以放在这里.

当时在vue3中,建立项目时没有static文件夹,这时若是有下载帮助文档的需求,就在public下新建static,然后将文件放在下面就可以了.

// 例如
 <a href='./static/helpFile.docx' download='帮助文档.docx'>帮助文档下载</a>
你好!关于Spring Boot和Vue文件上传与下载的问题,我可以为你提供一些解答。 在Spring Boot,你可以使用Spring MVC的文件上传功能来处理文件上传。首先,你需要在Spring Boot项目配置一个文件上传的Bean。可以使用`MultipartResolver`接口来实现文件上传功能。下面是一个示例代码: ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Bean public MultipartResolver multipartResolver() { return new StandardServletMultipartResolver(); } } ``` 接下来,你可以创建一个Controller来处理文件上传请求。你可以使用`@RequestParam`注解来接收上传的文件。以下是一个简单的上传文件的示例: ```java @RestController public class FileController { @PostMapping("/upload") public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) { // 处理文件上传逻辑 // 返回上传成功的消息 return ResponseEntity.ok("文件上传成功!"); } } ``` 在Vue,你可以使用`axios`库来发送文件上传请求。以下是一个简单的文件上传的示例代码: ```javascript <template> <div> <input type="file" ref="fileInput" @change="uploadFile"> </div> </template> <script> import axios from 'axios'; export default { methods: { uploadFile() { const file = this.$refs.fileInput.files[0]; const formData = new FormData(); formData.append('file', file); axios.post('/upload', formData) .then(response => { // 处理上传成功的逻辑 console.log(response.data); }) .catch(error => { // 处理上传失败的逻辑 console.error(error); }); } } } </script> ``` 至于文件下载,你可以在Spring Boot创建一个Controller来处理下载请求。你可以使用`ResponseEntity`来返回文件内容和正确的HTTP头。以下是一个简单的文件下载的示例: ```java @RestController public class FileController { @GetMapping("/download") public ResponseEntity<Resource> downloadFile() throws IOException { // 加载文件资源 Resource fileResource = new ClassPathResource("path/to/file.pdf"); // 设置HTTP头 HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=file.pdf"); return ResponseEntity.ok() .headers(headers) .body(fileResource); } } ``` 在Vue,你可以使用`window.open`方法来下载文件。以下是一个简单的文件下载的示例代码: ```javascript <template> <div> <button @click="downloadFile">下载文件</button> </div> </template> <script> export default { methods: { downloadFile() { const downloadURL = '/download'; window.open(downloadURL, '_blank'); } } } </script> ``` 希望以上信息能够帮助到你!如果有任何问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

绅士的可怖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值