jsp中的文件下载警报
我使用jsp中的以下代码从服务器下载文件。我正在使用struts框架。如果文件不存在,我想显示一条警告消息(使用以下代码,如果文件不存在,则以不可读的格式下载)。我不知道如何实施。如果有人能帮上忙,我会非常感激的 jsp内容:jsp中的文件下载警报,jsp,struts2,struts,struts-1,Jsp,Struts2,Struts,Struts 1,我使用jsp中的以下代码从服务器下载文件。我正在使用struts框架。如果文件不存在,我想显示一条警告消息(使用以下代码,如果文件不存在,则以不可读的格式下载)。我不知道如何实施。如果有人能帮上忙,我会非常感激的 jsp内容: <a href='<%=url%>/download.jsp?Path=<%=filePath%>&fileName=${CustomerRegistrationForm.customerId}_certificate.pdf' st
<a href='<%=url%>/download.jsp?Path=<%=filePath%>&fileName=${CustomerRegistrationForm.customerId}_certificate.pdf' style="text-decoration:none"><font color="#0000FF">Click Here</font></a>
单击url而不是href发送ajax请求。并检查响应状态是否为200,这意味着文件在那里并且可以下载。如果文件不在那里,您将获得404错误状态。在这种情况下,您可以提醒消息
<body>
<script language="javascript" type="text/javascript">
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
if(ajaxRequest.status == 200) {
alert("File Found");
document.getElementById("downloadIframe").src = "abc.csv";
}
else
alert("File Not Found");
}
}
ajaxRequest.open("GET", "abc.csv", true);
ajaxRequest.send(null);
}
</script>
<input type = "button" onclick = "ajaxFunction()" value = "Download"/>
<iframe id = "downloadIframe" src ="" style="display:none;"></iframe>
</body>
函数ajaxFunction(){
var ajaxRequest;//使Ajax成为可能的变量!
试一试{
//Opera 8.0+、Firefox、Safari
ajaxRequest=新的XMLHttpRequest();
}捕获(e){
//Internet Explorer浏览器
试一试{
ajaxRequest=newActiveXObject(“Msxml2.XMLHTTP”);
}捕获(e){
试一试{
ajaxRequest=新的ActiveXObject(“Microsoft.XMLHTTP”);
}捕获(e){
//出了点问题
警告(“你的浏览器坏了!”);
返回false;
}
}
}
//创建一个函数,该函数将接收从服务器发送的数据
ajaxRequest.onreadystatechange=函数(){
if(ajaxRequest.readyState==4){
如果(ajaxRequest.status==200){
警报(“找到文件”);
document.getElementById(“downloadIframe”).src=“abc.csv”;
}
其他的
警报(“未找到文件”);
}
}
打开(“GET”、“abc.csv”、true);
ajaxRequest.send(空);
}
我想在同一个jsp页面中实现它。谢谢Konza。事实上,我对ajax一无所知。我只使用jsp、html和java。不确定如何在我上面提到的代码中实现上述代码。如果可能,请给我完整的代码。请尝试此链接。。这很容易。。没有什么可学的@我已经编辑了答案。请检查一下。这是一个有效的例子。。。希望这对你有帮助。