Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jhipster应用程序中使用window.location下载文件_Javascript_Angularjs_Jhipster - Fatal编程技术网

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

Javascript 在jhipster应用程序中使用window.location下载文件,javascript,angularjs,jhipster,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);

我想从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();
}
我希望用户现在能够下载该文件

在我的一个页面的控制器中,我添加了此功能,以测试下载(或在浏览器中直接键入url):

但我被重定向到主页,服务器端和客户端都没有执行任何操作


您能帮我允许url上的请求吗?

http://localhost:8080/#/file/123
http://localhost:8080/file/123

前者只是加载
http://localhost:8080/
/file/123
作为http://localhost:8080/file/123实际上会向服务器发送请求,请求
/file/123
路径

要下载文件,您需要将用户导航到后者:

window.location = "http://localhost:8080/file/" + id;

也就是说,您可能不想让他们离开您的应用程序-在这种情况下,您最好使用。

如果我尝试不使用,我会得到一个404未找到的错误(即使在我的应用程序的其他url上)。这是一个用Jhipster生成的应用程序。@C.Cam:那些有几个问题混合在一起的问题总是很难解决-很高兴我们能提供帮助:)您是否检查过您的类没有像Jhipster生成的资源那样的
@RequestMapping(“/api”)
注释?
window.location = "http://localhost:8080/file/" + id;