ajax - 使用 Spring Boot 和 Java 脚本从应用程序下载压缩的父文件夹时出错
问题描述
我正在开发 Spring Boot 应用程序,我需要在其中提供下载压缩报告文件功能。为了做到这一点,我将压缩报告文件转换为字节数组,然后尝试在浏览器端发送它。我可以下载压缩的报告文件,但是当我尝试提取它时会显示错误消息“压缩的压缩文件夹无效。” 请在下面找到代码片段:
服务器端代码:
@ResponseBody
@RequestMapping(value = "/downloadResult", method = RequestMethod.POST, produces="application/zip" )
public byte[] downloadResult(@RequestBody ResultParamsVO resultParamsVO, HttpServletRequest request,
HttpServletResponse response) {
byte[] result = null;
try {
HttpSession session = request.getSession(false);
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String user = auth.getPrincipal().toString();
String testType = null;
if (session.getAttribute("testType") != null) {
testType = session.getAttribute("testType").toString();
}
result = jobService.downloadResult(resultParamsVO, testType, user);
response.setStatus(HttpServletResponse.SC_OK);
response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=resultDownload1.zip");
} catch (Exception e) {
logger.error("Error in getResult() for job id - " + resultParamsVO.getJobId(), e.getMessage());
e.printStackTrace();
}
return result;
}
浏览器端 Ajax 调用:
$.ajax({
type: "POST",
url: "/downloadResult",
contentType: "application/json",
data: JSON.stringify(resultParamsVO),
success: function(result) {
var downloadUrl = window.URL.createObjectURL(new Blob([result], {type: "application/zip"}));
var a = document.createElement("a");
a.href = downloadUrl;
a.download = "resultDownload.zip";
document.body.appendChild(a);
a.click();
}
});
请帮忙。提前致谢。
解决方案
推荐阅读
- angular - jhipster 和当前日期显示和检查
- scala - 如何针对 Long 值类型优化我的 properFractions 函数
- tinymce - 从 TinyMCE 获取 HTML 标记?
- java - 替换 Jenkins 的扩展选择或复选框的活动选择分隔符
- python-3.x - HTTPError:HTTP 错误 403:在从 Python3 中的链接下载 csv 文件期间定义标头时返回 Forbidden 或 None
- azure - 启用对 API 的 Azure 可用性测试访问
- python - 如何对推文进行分类(支持与不支持)以预测选举结果
- google-cloud-firestore - 设置文档失败时,如何在我的 firebase 函数中发现错误?
- linux - 最大化 linux 原始套接字吞吐量
- amazon-web-services - 是否可以在 2 个存储桶之间复制特定的 S3 文件夹?