赞
踩
若依下载的逻辑是指定文件存储的路径,在ruoyi-admin模块下的application.yml中配置,路径结尾必须要加'/'或者'\'结尾。
他使用的是虚拟路径映射,所以文件名必须是配置路径下真实的文件名。
若依采用的是流的方式,前端必须要用bolb的方式去接收,然后保存成本地文件。
参数一:配置路径下,真实的文件名。
参数二:是否下载后删除,false不删除。
- @Autowired
- RuoYiConfig RuoYiConfig;
-
- @GetMapping("/downloadZip")
- public void fileDownload(@RequestParam String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
- try {
- System.out.println("文件名称:" + fileName);
- if (!FileUtils.checkAllowDownload(fileName)) {
- throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
- }
- String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
- String filePath = RuoYiConfig.getDownloadPath() + fileName;
-
- response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
- FileUtils.setAttachmentResponseHeader(response, realFileName);
- FileUtils.writeBytes(filePath, response.getOutputStream());
- if (delete) {
- FileUtils.deleteFile(filePath);
- }
- } catch (Exception e) {
- e.printStackTrace();
- System.out.format("下载文件失败", e);
- }
- }

- import request from '@/utils/request'
-
- export const publishExportTemplateFile = (res, name, type = false) => {
- let url;
- if (type) {
- url = window.URL.createObjectURL(new Blob([res], {type: 'application/zip'}));
- } else {
- url = window.URL.createObjectURL(new Blob([res]));
- }
- // const url = window.URL.createObjectURL(new Blob([res]));
- const link = document.createElement('a');
- link.style.display = 'none';
- link.href = url;
- link.setAttribute('download', name);
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link);
- }
-
- export function downloadFile(filePath) {
- request({
- url: '/projectAch/pages/downloadZip',
- method: 'get',
- params: {fileName: filePath, delete: false},
- responseType: 'blob' // 设置响应类型为 blob
- })
- .then(response => {
- publishExportTemplateFile(response ,filePath);
- })
- .catch(error => {
- console.error('Download file failed:', error);
- });
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。