我想在 Jhipster 应用程序中从服务器下载文件 (.docx)。 我直接从服务器发回二进制内容。
@GetMapping("/file/{id}")
@Timed
public void getFile(@PathVariable Long id, HttpServletResponse response) throws URISyntaxException, IOException {
FileInputStream stream = fileService.getFile(id);
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
IOUtils.copy(stream,response.getOutputStream());
stream.close();
}
我现在希望用户能够下载该文件。
在我的一个页面的 Controller 中,我添加了此功能,以测试下载(或者我直接在浏览器中输入网址):
function dwl (id) {
window.location = "http://localhost:8080/#/file/"+id;
}
但是我被重定向到主页,并且服务器端和客户端均未执行任何操作。
您能帮我允许该网址上的请求吗?
最佳答案
http://localhost:8080/#/file/123
与 http://localhost:8080/file/123 不同。
.
前者只是加载 http://localhost:8080/
和 /file/123
作为 location hash ,而 http://localhost:8080/file/123
实际上向您的服务器发送了 /file/123
路径的请求。
要下载您的文件,您需要将用户导航到后者:
window.location = "http://localhost:8080/file/" + id;
也就是说,您可能不希望他们离开您的应用程序 - 在这种情况下,您最好使用 window.open
.
关于javascript - 在 jhipster 应用程序中使用 window.location 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42972483/