从使用Ajax的Servlet下载文件 [英] Download a file from Servlet using Ajax

查看:259
本文介绍了从使用Ajax的Servlet下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了我的servlet的zip文件。现在我想使用Ajax来触发这个servlet并提示下载对话框的用户。我可以触发这个servlet,但我不知道怎么去保存对话框。我怎样才能做到这一点?

I have created a zip file in my servlet. Now I would like to trigger that servlet using Ajax and prompt the download dialog to the user. I can trigger the servlet, but I don't know how to get the save dialog. How can I achieve this?

推荐答案

您无法下载使用AJAX的文件。 AJAX是有关从服务器的JavaScript处理下载数据。

You can't "download a file using AJAX". AJAX is about downloading data from a server for JavaScript to process.

要允许用户下载该文件或者使用一个简单的链接到文件/ servlet或如果你真的,真的需要使用JavaScript,然后将URL分配给 document.location.href

To let the user download the file either use a simple link to the file/servlet, or if you really, really need to use JavaScript, then assign the URL to document.location.href.

另外,你需要确保服务器(或在这种情况下,该servlet)发送相应的MIME类型,情况最有可能的一个ZIP文件的应用程序/压缩

Also you need to make sure that the server (or in this case the servlet) sends the appropriate MIME type, in case of a ZIP file most likely application/zip.

这篇关于从使用Ajax的Servlet下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆