Vue下载excel文件模板,Vue 下载本地静态资源static文件夹

在Vue CLI3项目中,由于打包时assets目录下的文件路径不固定,为实现本地docx文档下载,需将文件放在public/static下。通过`<a>`标签的href属性指定 `/static/serviceAgreement.docx` 的路径,并设置download属性实现下载。注意不同Vue CLI版本对静态资源位置的要求,Vue CLI2需要放在static,而CLI3及以上版本则应放入public。
 

下载静态文件方法:

 <a href="/static/serviceAgreement.docx" download="下载"></a>

下载;

项目需要下载本地的docx文档,文档是放在本地的没有在服务器,所以需要下载本地静态资源文件,开始把文件放在了这里src目录下的assets资源文件


下载报错 找不到文件路径查找原因是因为项目用的是vue-cli3, 在打包的时候并不知道会把assets

下的文件打包在哪里,但是在build的时候发现在根目录下的文件是打包在当前路径下的
所以我们要把需要下载的静态资源放在public文件夹下的static文件夹下就可以了

你可能在输入路径的时候会提示你是src,而不直接是static,但是你还是要直接输入static这个路径
 

 <a href="/static/serviceAgreement.docx" download="服务协议"></a>

需要注意的是:cli2需要把要下载的文件放在static下面;cli4需要将文件放在public里面才不会被打包。

 注意:href 指向项目中pdf文件的路径(不要出现中文),download 就是重命名的文件名,不设置的话就是默认文件名

发现vue版本是 3.0+ 所有把要下载的 文件直接放到public文件夹中(最好英文名字)
                        
原文链接:https://blog.csdn.net/qq_44848480/article/details/123658845

Vue下载excel文件模板

注意:

1.模板一定是英文名

2.使用相对于index.html的路径

3.a标签同理

<a href="./static/template.xlsx" download="模板.xlsx"></a>

第一步:

 vue2.0版本的在项目根目录下的static文件夹,放入“文件模板.xlsx”文件。

 vue3.0版本的在项目public目录下新建static文件夹,放入“文件模板.xlsx”文件。

第二步:

  在按钮中加入事件

<el-button icon="el-icon-download" size="medium" @click="downloadTemplate">模板下载</el-button>

   downloadTemplate() {
      // 使用相对路径
      const fileUrl = '/static/template.xlsx'';
      // 创建一个隐藏的a标签
      const link = document.createElement('a');
      link.href = fileUrl;
      link.download = 'template.xls';  // 设置下载文件名
      // 将a标签添加到DOM并触发点击事件
      document.body.appendChild(link);
      link.click();
      // 移除a标签
      document.body.removeChild(link);
    },

Vue下载excel文件模板 - 往暮 - 博客园

好的,以下是代码实现: 前端代码: ```vue <template> <div> <button @click="downloadFile">下载文件</button> </div> </template> <script> export default { methods: { downloadFile() { window.open("/statics.zip"); }, }, }; </script> ``` 后端代码: ```java package com.example.demo.controller; import org.springframework.boot.configurationprocessor.json.JSONException; import org.springframework.boot.configurationprocessor.json.JSONObject; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileSystemResource; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @Controller public class FileDownloadController { @GetMapping("/download/statics") @ResponseBody public ResponseEntity<byte[]> downloadStaticResources() throws IOException { FileSystemResource fileSystemResource = new FileSystemResource(compressStaticResources()); byte[] fileBytes = new byte[(int) fileSystemResource.getFile().length()]; FileInputStream inputStream = new FileInputStream(fileSystemResource.getFile()); inputStream.read(fileBytes); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", "statics.zip"); headers.setContentLength(fileBytes.length); return ResponseEntity.ok().headers(headers).body(fileBytes); } private String compressStaticResources() { String tempZipPath = System.getProperty("java.io.tmpdir")+"/statics.zip"; try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tempZipPath))) { for (String staticsFolder : new String[]{"static", "public"}) { try { addFolderToZip(new ClassPathResource(staticsFolder).getFile(), staticsFolder, zos); } catch (IOException e) { e.printStackTrace(); } } return tempZipPath; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } private void addFolderToZip(File folder, String parentFolderName, ZipOutputStream zipOutputStream) throws IOException { if (folder.isDirectory()) { for (File file : folder.listFiles()) { addFolderToZip(file, parentFolderName, zipOutputStream); } } else { String name = folder.getName(); String path = folder.getPath().replace("\\", "/"); zipOutputStream.putNextEntry(new ZipEntry(path.substring(path.indexOf(parentFolderName), path.length()))); FileInputStream fileInputStream = new FileInputStream(folder); byte[] bytes = new byte[1024]; int length; while ((length = fileInputStream.read(bytes)) >= 0) { zipOutputStream.write(bytes, 0, length); } fileInputStream.close(); } } } ``` 这段代码使用了ZipOutputStream,将指定的静态资源文件夹进行了压缩,并通过@RepositoryRestController注解的Controller接口提供了下载静态资源文件的功能。 你可以在前端页面点击下载文件按钮,将会自动下载静态资源文件。同时,该代码也适用于任意SpringBoot+Vue项目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值