index.jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="css/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/jquery.download.js"></script>
<script type="text/javascript">
function request(){
var params={
fileName:'bb.txt'
};
var iframe = $('<iframe name="download" style="position:absolute;top:-9999px" />');
iframe.appendTo('body');
var form = $('<form name="form_download" method="post" style="display:none;" enctype="multipart/form-data"/>');
form.attr("target","download").attr('action',"/test/demo/downloadFile.action");
var formHtml='';
/** 向form中添加元素*/
for (var key in params) {
formHtml+='<input type="hidden" value="'+params[key]+'" name="'+key+'"/>';
}
console.log(formHtml);
form.append(formHtml);
form.appendTo('body');
form.submit();
}
function test(){
/*$.download({
url: '/test/demo/downloadFile.action',
dataType: 'json',
params: {
'fileName': 'bb.txt'
},
onSend: function () {
return true;
},
onComplate: function (data) {
alert(data.msg);
}
});*/
alert('sdfdsfdsf');
}
</script>
<title>ajax测试</title>
</head>
<body>
<a href="#" onclick="request();">linkBtn</a>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------------------------------
action代码:
package com.action;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
// 下载文件
/**
* @return
* @throws Exception
*/
public String downloadFile(){
OutputStream out = null;
HttpServletResponse response = null;
String msg=null;
try {
HttpServletRequest request = ServletActionContext.getRequest();
response = ServletActionContext.getResponse();
out = response.getOutputStream();
// Integer.parseInt("1222222222222");
String fileName = request.getParameter("fileName");
InputStream in = null;
if (StringUtils.hasLength(fileName)) {
in = HelloAction.class.getResourceAsStream("/"+fileName);
if (null != in) {
response.setContentType("application/octet-stream;charset=utf-8");
response.setHeader("Content-disposition","attachment;filename=\""+fileName+"\"");
FileCopyUtils.copy(in,out);
}
}
} catch (Exception e) {
e.printStackTrace();
try {
//msg="{\"success\":\"false\",\"msg\":\"download Failed!\"}";
msg="<script>parent.window.test();</script>";
out.write(msg.getBytes());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return null;
}
}
参考js代码:
var frameName = 'download_frame_' + (frameCount++); var iframe = $('<iframe style="position:absolute;top:-9999px" />').attr('name',frameName); //创建form var form = $('<form method="post" style="display:none;" enctype="multipart/form-data" />').attr('name','form_' + frameName); form.attr("target",frameName).attr('action',opts.url); //向form中放元素 var formHtml = ''; for (key in opts.params) { formHtml += '<input type="hidden" name="' + key + '" value="' + opts.params[key] + '">'; } form.append(formHtml); iframe.appendTo("body"); form.appendTo("body"); form.submit();
原文链接:https://www.f2er.com/ajax/165342.html