[size=medium]
用POST请求让浏览器下载文件
方法1. 动态构造一个html Form表单元素,将参数隐藏在表单中通过post传过去
[/size]
[size=medium]
方法2. 先在服务器端生成文件, 这样你就面临一个如何清除这些文件的问题
[url]http://www.sencha.com/forum/showthread.php?81897-FYI-Very-simple-approach-to-JS-triggered-file-downloads[/url]
[url]http://stackoverflow.com/questions/3499597/javascript-jquery-to-download-file-via-post-with-json-data[/url]
[url]http://www.sencha.com/forum/showthread.php?153253-How-to-download-a-file-using-Ext.Ajax-using-a-POST-call[/url]
其他参考:
用window.location.href=''方法打开页面下载打印数据
[url]http://yuzhijia88-126-com.iteye.com/blog/747142[/url]
用GET请求让浏览器下载文件
客户端:
很简单,就是拼凑服务器端的URL,然后window.open()就行
var url = getContextURL() + 'services/exportExcel?reportDate=20151110'
window.open(url);
服务器端,这里包含了使用一个excel模板来生成最终的文件:[/size]
用POST请求让浏览器下载文件
方法1. 动态构造一个html Form表单元素,将参数隐藏在表单中通过post传过去
[/size]
var data = tableElement.data;
var columns = tableElement.columns;
var groupColumns = tableElement.groupColumns;
var url = 'servlet/exportMovementReport.do';
var name = 'download';
var keys = ['data', 'columns', 'groupColumns'];
var values = [Ext.encode(data), Ext.encode(columns), Ext.encode(groupColumns)];
var html = "";
html += "<html><head></head><body><form id='formId' name='formName' method='post' action='"
+ url + "'>";
if (keys && values && (keys.length == values.length)) {
for (var i = 0; i < keys.length; i++) {
html += "<input type='hidden' name='" + keys[i] + "' value='"
+ values[i] + "'/>";
}
}
html += "</form><script type='text/javascript'>document.getElementById(\"formId\").submit()</script></body></html>";
window.open('#', name).document.write(html);
[size=medium]
方法2. 先在服务器端生成文件, 这样你就面临一个如何清除这些文件的问题
[url]http://www.sencha.com/forum/showthread.php?81897-FYI-Very-simple-approach-to-JS-triggered-file-downloads[/url]
[url]http://stackoverflow.com/questions/3499597/javascript-jquery-to-download-file-via-post-with-json-data[/url]
[url]http://www.sencha.com/forum/showthread.php?153253-How-to-download-a-file-using-Ext.Ajax-using-a-POST-call[/url]
其他参考:
用window.location.href=''方法打开页面下载打印数据
[url]http://yuzhijia88-126-com.iteye.com/blog/747142[/url]
用GET请求让浏览器下载文件
客户端:
很简单,就是拼凑服务器端的URL,然后window.open()就行
var url = getContextURL() + 'services/exportExcel?reportDate=20151110'
window.open(url);
服务器端,这里包含了使用一个excel模板来生成最终的文件:[/size]
@GET
@Path("/exportExcel")
public Response exportExcel(@QueryParam("reportDate") String reportDate, @Context HttpServletResponse response) throws IOException, InvalidFormatException
{
List<Map<String, Object>> data = ...;
Map<String,Object> dataMap = Maps.newHashMap();
dataMap.put("notifications", data);
dataMap.put("reportDate", reportDate);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=\"Export_Report.xls\"");
try {
InputStream is = new ClassPathResource("template/Export_Template.xls").getInputStream();
XLSTransformer transformer = new XLSTransformer();
org.apache.poi.ss.usermodel.Workbook wb = transformer.transformXLS(is, dataMap);
wb.write(response.getOutputStream());
} catch (IOException e) {
LOGGER.error("Failed to read template!", e);
throw e;
} catch (ParsePropertyException e) {
LOGGER.error("Failed to read template!", e);
throw e;
} catch (InvalidFormatException e) {
LOGGER.error("Failed to read template!", e);
throw e;
}
return Response.ok().build();
}