javascript 如何让文件下载而不是在浏览器中打开?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10974734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make files download instead of opening in browser?
提问by anto
I am a new bie, i want files to get downloaded when user clicks on download option its opening in the browser instead of download option like save as/open.Here i referred for the same and every where they have suggested to use
我是一个新手,我希望在用户点击下载选项时下载文件,它在浏览器中打开而不是像另存为/打开这样的下载选项。在这里我提到了他们建议使用的相同和每个地方
Response.AddHeader("Content-disposition", "attachment; filename=" + Name);
But i don't know where and how to use. Actually i'm getting the url value from query written which return url as one of the object of bean stored in arraylist(This list is having other values also with url ). I am having the url values inside the arraylist as bean as like
但我不知道在哪里以及如何使用。实际上,我正在从所写的查询中获取 url 值,该值返回 url 作为存储在 arraylist 中的 bean 对象之一(此列表也有其他值与 url )。我在arraylist中的url值像bean一样
type=.pdf
release date=12/3/08
name=hai.pdf
url=/files/en/soft/doc/docs/hai.pdf
I am getting this array list in my controller like this
我像这样在我的控制器中得到这个数组列表
ArrayList details = dao.getdetails(Bean.getNumber());
and pass this into view like this
并像这样将其传递到视图中
Map.put("details", details);
modelView.setViewName("details_list");
modelView.addAllObjects(Map);
return modelView;
in jsp i have iterated this array list and diplays the content like this
在jsp中我已经迭代了这个数组列表并像这样显示内容
Type name Release Date
.txt hai.pdf May 21st 2012 Download
.txt hello.txt May 21st 2012 Download
For download i have used like this in jsp
对于下载,我在jsp中使用过这样的
<td colspan="2" valign="top">
<a href="${details.Url}"/>
<img src="/images/download.gif" alt="Download" border="0" align="right"></a>
</td>
here on click of download its opening in browser.I need this to be downloaded instead. Please help me in how to use or handle
点击下载它在浏览器中打开。我需要下载这个。请帮助我如何使用或处理
response.setHeader("Content-Disposition", "attachment;");
where to add the above for my requirement or if i can do with any java script also.Please help me in solving the above.
在哪里添加以上内容以满足我的要求,或者我是否也可以使用任何 java 脚本。请帮助我解决上述问题。
回答by Santosh
Here is one way of doing it:
这是一种方法:
- Create a web Filter(or this way )
- Map this filter to the PDF URL.
- In the
doFilter()
method, set the response header for content download.
Example :
例子 :
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain chain)
throws IOException, ServletException {
String name = request.getAttribute("filename");
response.addHeader("Content-disposition", "attachment; filename=" + name);
chain.doFilter(request, response);
}
You can set the filename as an request attribute (reqest.setAttribute()) from your controller class
您可以将文件名设置为控制器类的请求属性 (reqest.setAttribute())
Filters are pretty standard in Java web stack.
过滤器在 Java 网络堆栈中是非常标准的。
回答by ankit
It's depend on the header which browser get in response.
这取决于浏览器响应的标头。
Suppose if header is image/png then browser will show it. Same way if you send same image with application/octet-stream then browser will force to download it.
假设如果标题是 image/png 则浏览器将显示它。如果您使用 application/octet-stream 发送相同的图像,则浏览器将强制下载它。
take a look http://en.wikipedia.org/wiki/Byte_stream
看看http://en.wikipedia.org/wiki/Byte_stream
In a project I have figure out that sending request from browser are different.
在一个项目中,我发现从浏览器发送请求是不同的。
If you upload a image from Firefox or IE then it's will upload them as image/png wherever chrome upload them as application/octet-stream.
如果您从 Firefox 或 IE 上传图像,那么无论 chrome 将它们上传为应用程序/八位字节流,它都会将它们上传为图像/png。
回答by Naresh
just try to add header
只是尝试添加标题
response.setHeader("Content-Type: application/force-download");
response.setHeader("Content-Type: application/force-download");