javascript - 在 jhipster 应用程序中使用 window.location 下载文件

标签 javascript angularjs jhipster

我想在 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/123http://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/

相关文章:

javascript - 如何防止在某些情况下显示剑道工具提示?

javascript - dc.js - 使用 jquery 数据表插件的数据表

javascript - jQuery 日期选择器不显示

javascript - Angular2 http.get 预检问题

javascript - 如何使用 AngularJS 连接 ng-app

yeoman - 使用旧版本的jHipster

java - Jhipster Elasticsearch数据迁移

javascript - 完成渲染层事件

javascript - 带有复选框和搜索方法的多选下拉菜单

jpa - 如何在 JHipster 中为实体添加唯一约束?