当前位置: 首页 > news >正文

vue+springboot 前后端分离 上传文件处理后再下载,并且传递参数

vue代码

<template><div><input type="file" ref="fileInput" accept=".json"/><el-button @click="upload">上传</el-button></div>
</template><script>
export default {name: "Upgrade",data() {return {}},methods: {upload() {const files = this.$refs.fileInput.filesvar formData = new FormData();let config = {headers: {"Content-Type": "multipart/form-data",},responseType: "blob", //这个很重要};formData.append("file", files[0]);formData.append("fromVersion", "3.2.1")formData.append("toVersion", "4.0.0")this.axios.post("/upgrade/apiConfig", formData, config).then((response) => {const link = document.createElement("a");let blob = new Blob([response.data], {type: "application/x-msdownload"});link.style.display = "none";link.href = URL.createObjectURL(blob);link.setAttribute("download", "apiConfig.json");document.body.appendChild(link);link.click();document.body.removeChild(link);});}}
}
</script><style scoped lang="less"></style>

springboot代码

@RequestMapping(value = "/apiConfig", produces = "application/json;charset=UTF-8")public void convertApiConfig(@RequestParam("file") MultipartFile file, @RequestParam("fromVersion") String fromVersion, @RequestParam("toVersion") String toVersion, HttpServletResponse response) {OutputStream os = null;try {String s = IOUtils.toString(file.getInputStream(), "utf-8");String content = upgradeService.upgradeApiConfig(s, fromVersion, toVersion);response.setContentType("application/x-msdownload;charset=utf-8");response.setHeader("Content-Disposition", "attachment; filename=api_config.json");os = response.getOutputStream();os.write(content.getBytes("utf-8"));} catch (Exception e) {e.printStackTrace();} finally {try {if (os != null)os.close();} catch (IOException e) {e.printStackTrace();}}}
http://www.lryc.cn/news/126084.html

相关文章:

  • 【Linux操作系统】举例解释Linux系统编程中文件io常用的函数
  • Ubuntu和centos版本有哪些区别
  • Netty:ChannelHandler抛出异常,对应的channel被关闭
  • pytest结合 allure 打标记之的详细使用
  • 【linux】2 软件管理器yum和编辑器vim
  • Angular中的ActivatedRoute和Router
  • Layui精简版,快速入门
  • SSH远程Ubuntu教程
  • NPM与外部服务的集成(下)
  • Flask Web开发实战(狼书)| 笔记第1、2章
  • PHP利用PCRE回溯次数限制绕过某些安全限制实战案例
  • 读书笔记 |【项目思维与管理】➾ 顺势而动
  • 开发利器:接口代理和接口模拟工具
  • MAVEN利器:一文带你了解MAVEN以及如何配置
  • 解决 adb install 错误INSTALL_FAILED_UPDATE_INCOMPATIBLE
  • 学习Vue:Event Bus 与 Provide/Inject
  • Java 工具类之JSON key根据ASCII排序
  • 深兰科技提出新多模态谣言监测模型,刷新世界纪录
  • 【从零学习python 】33.装饰器的作用(二)
  • 【自动电压调节器】无功功率控制的终端电压控制研究(Simulink)
  • 比ChatGPT更强的星火大模型V2版本发布!
  • Character Animation With Direct3D 读书笔记
  • SpringBoot之HandlerInterceptor拦截器的使用
  • 【共同缔造 情暖襄阳】 暑期关爱未成年人志愿服务活动合集(三)
  • 私密相册管家-加密码保护私人相册照片安全
  • webpack 热更新的实现原理
  • OpenCV-Python中的图像处理-傅里叶变换
  • 阿里云FRP内网穿透挂载多台服务器
  • 多店铺功能
  • mysql主从复制搭建(一主一从)