制作<a href=""> tag downloadable by passing a variable?

时间:2018-03-21 06:14:45

标签: java html mysql jsp

How can I pass a java variable to a tag and make it downloadable ? I have tried the below code. But unfortunately it opens the file, rather than downloading it.

 out.println("<a href=" + path + ">" + fileName + " download</a>" + "<BR>");

Please follow the full code,

 while (rs.next()) {
                System.out.println(rs.getString("location"));
                path = rs.getString("location");
                fileName = rs.getString("fileName");
                out.print(path + "<BR>");
                out.println("<a href=" + path + ">" + fileName + " download</a>" + "<BR>");
            }

2 个答案:

答案 0 :(得分:4)

最新的chrome(来自65)禁用下载文件,该文件与您访问的网站不同。在chrome 65之前,您可以下载文件,只需将下载属性添加到<a>标记。

提交链接为https://chromium.googlesource.com/chromium/src/+/2a6afcb26ba6cd2324ddaa366b11968237e304a3%5E%21/#F0

因此,如果您仍想下载该文件,您请求的链接应返回响应标题

Content-Disposition: attachment; filename=filename.ext

答案 1 :(得分:1)

当您单击该链接时,服务器需要发送respnose标头Content-Disposition

Content-Type: application/octet-stream
Content-Disposition: attachment; filename="picture.png"
相关问题