JAVA配合Freemark模板生成word返回文件流,前台vue接收下载

直接上vue的代码:

	let sendData = {}
    let successCallBack = (resp) => {
      if (resp.status !== 200) {
        this.$message.error('文件下载失败')
        return false
      }
      // ********************* 接收文件流 ***********************
      let blob = new Blob([resp.data], {
        // type   mimi类型  可以百度详细类型
        // word文档为msword,application/pdfpdf文档为pdf  application/vnd.ms-excel
        // image/jpeg jpg
        //  type: `msword`
        type: `application/msword`	// 'application/octet-stream' 代表所有扩展名,这里也可以不指定 type
      })
      let objectUrl = URL.createObjectURL(blob)
      let link = document.createElement('a')
      // 这里需要注意:
      //     取出来的fName可能带引号,如 "xxx.png", 这种情况需要再处理一下:fName.slice(1, -1)
      let fName = decodeURI(resp.headers['content-disposition'].split('=')[1])
      link.href = objectUrl
      link.setAttribute('download', fName)
      document.body.appendChild(link)
      link.click()
      // 下载完成移除元素
      document.body.removeChild(link)
      // 释放掉blob对象
      window.URL.revokeObjectURL(objectUrl)
    }
    // this.$request.xxx()  是因为我将axios重新进行了封装
    this.$Request.getFiles({
      data: sendData,
      responseType: 'blob',	// **********这里的响应类型必须设置为'blob'
      success: successCallBack,
      requestDataType: 'form-data'
    })
  }

更详细的请参考:https://blog.csdn.net/CarryBest/article/details/102586653

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当您在Spring Boot使用Freemarker生成HTML表格时,需要注意以下几点: 1. 确认您已经在项目引入了Freemarker依赖。 2. 在Spring Boot配置文件配置Freemarker的相关属性。 3. 编写Freemarker模板,指定表格的数据和样式。 下面是一个简单的示例,演示如何在Spring Boot使用Freemarker生成HTML表格: 1. 在项目引入Freemarker依赖。您可以在 `pom.xml` 文件添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> ``` 2. 在 Spring Boot 的配置文件(如 `application.properties`),添加以下Freemarker配置: ``` spring.freemarker.template-loader-path=classpath:/templates/ spring.freemarker.suffix=.ftl ``` 3. 编写Freemarker模板。以下示例展示了如何使用Freemarker动态生成HTML表格: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Freemarker Example</title> </head> <body> <table> <thead> <tr> <th>Name</th> <th>Age</th> <th>Gender</th> </tr> </thead> <tbody> <#list users as user> <tr> <td>${user.name}</td> <td>${user.age}</td> <td>${user.gender}</td> </tr> </#list> </tbody> </table> </body> </html> ``` 在这个示例,我们使用了Freemarker的模板语法,通过 `${}` 表达式来输出表格的数据。我们使用了 `<#list>` 指令来遍历用户列表,生成表格的每一行。 4. 在Java代码,使用Freemarker生成HTML表格。以下示例展示了如何在Spring Boot使用Freemarker生成HTML表格: ```java @Autowired private Configuration freemarkerConfig; @GetMapping("/users") public String getUsers(Model model) throws IOException, TemplateException { List<User> users = userService.getUsers(); Template template = freemarkerConfig.getTemplate("users.ftl"); String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, "users", users); model.addAttribute("html", html); return "users"; } ``` 在这个示例,我们使用了Spring Boot的`Configuration`类来获取Freemarker模板引擎的实例。我们调用了`getTemplate`方法来获取指定名称的模板,然后使用`FreeMarkerTemplateUtils`类的`processTemplateIntoString`方法将数据填充到模板生成HTML字符串。 最后,我们将生成的HTML字符串添加到`Model`返回前端页面进行渲染。 希望这个示例能帮助您在Spring Boot使用Freemarker生成HTML表格。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值