简体   繁体   English

下载带有HTTP标头的文件

[英]Download file with http header

I'm looking for dowloading PDF files with html links with HTTP authentication header. 我正在寻找使用带有HTTP身份验证标头的html链接下载PDF文件的方法。

It is not possible to fix them without javascript, so I tried this : 没有JavaScript不可能修复它们,所以我尝试了以下方法:

Html : HTML:

<a id="myLink" href="">fileName.pdf</a>

Javascript : Javascript:

$("#myLink").on("click", function() {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", myUrl, true);
        xhr.setRequestHeader("X-Token", authToken);
        xhr.send();
        return false;
});

File is downloaded (regards to network analysis) but I don't know how to open it in a new browser tab. 文件已下载(关于网络分析),但我不知道如何在新的浏览器选项卡中打开文件。 I know we don't save files from javascript but I just want to display it. 我知道我们不会保存来自javascript的文件,但我只想显示它。 Do you have any idea ? 你有什么主意吗 ?

Thanks. 谢谢。

Solution was to store header value inside a cookie. 解决方案是将标头值存储在cookie中。

When using RestEasy, cookie can be read inside an http filter (preProcess function for RestEasy 2.3.7.Final) with : 使用RestEasy时,可以使用以下命令在http过滤器(RestEasy 2.3.7.Final的preProcess函数)内部读取cookie:

request.getHttpHeaders().getCookies()

request is an HttpRequest object. request是一个HttpRequest对象。

There's still one "problem", files are never opened inside browser. 仍然存在一个“问题”,文件永远不会在浏览器中打开。 I don't know how to fix this yet. 我还不知道该如何解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM