showModelDialog的使用(二):提供下载功能
2016-04-14 22:12
295 查看
在上一篇是展示了showModelDialog的基本使用。当点击我要修改时,需要在后台生成excel文件,同时需要提供下载文件功能。
生成excel文件容易、弹出”文件下载”也容易:
点击按钮,跳转到action中,在该action中生成Excel文件,填充数据,保存到临时文件夹中,然后在按钮的Click事件中,根据模板生成Excel报表,填充数据,保存到临时文件夹,然后output.wirte()。一切看起来很顺利。
Action如下(弹出"文件下载"窗口):
[java] view
plain copy
print?
/**
* @param response
* @param downloadFile
*/
private void clientResponse(HttpServletResponse response,File downloadFile, String fileName){
try {
response.reset();
response.setContentType("application/octet-stream");
// 用来弹出保存窗口 ,设置 为attachment
response.setHeader("Content-Disposition", "attachment; filename="+ new String(fileName.getBytes(),"ISO-8859-1"));
InputStream input=new FileInputStream(downloadFile);
OutputStream output=response.getOutputStream();
int c;
// 读取流并写入到文件中
while ((c = input.read()) != -1) {
output.write(c);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
}
}
但是当测试的时候,发现我点击”我要修改”按钮,总是会弹出一个新的窗口。百度一下,加了这段:<base target="_self">,这个表示在当前页打开页面。
如下:
base: 为页面上的所有链接规定默认地址或默认目标
target: 跳转到的目标页
<base target=_blank><!-- 在空白页打开 -->
<base target=_parent> <!-- 在当前页的上一页(父类)打开 -->
<base target=_search> <!-- 在浏览器地址栏打开-->
<base target=_self> <!-- 在当前页打开-->
<base target=_top> <!-- 在最初(首页)页打开 -->
这个问题解决了,但是新的问题又来了,就是文件不可以下载。所以我有没有那种方法既可以在本页打开,又可以提供下载呢?想到了iframe框架。我们可以设置一个看不见的iframe框架,然后target=iframName就可以解决了。
[html] view
plain copy
print?
<iframe id="download" name="download" height="0px" width="0px"></iframe>
<base target="download">
这<base...>位于<head></head>之间
生成excel文件容易、弹出”文件下载”也容易:
点击按钮,跳转到action中,在该action中生成Excel文件,填充数据,保存到临时文件夹中,然后在按钮的Click事件中,根据模板生成Excel报表,填充数据,保存到临时文件夹,然后output.wirte()。一切看起来很顺利。
Action如下(弹出"文件下载"窗口):
[java] view
plain copy
print?
/**
* @param response
* @param downloadFile
*/
private void clientResponse(HttpServletResponse response,File downloadFile, String fileName){
try {
response.reset();
response.setContentType("application/octet-stream");
// 用来弹出保存窗口 ,设置 为attachment
response.setHeader("Content-Disposition", "attachment; filename="+ new String(fileName.getBytes(),"ISO-8859-1"));
InputStream input=new FileInputStream(downloadFile);
OutputStream output=response.getOutputStream();
int c;
// 读取流并写入到文件中
while ((c = input.read()) != -1) {
output.write(c);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
}
}
但是当测试的时候,发现我点击”我要修改”按钮,总是会弹出一个新的窗口。百度一下,加了这段:<base target="_self">,这个表示在当前页打开页面。
如下:
base: 为页面上的所有链接规定默认地址或默认目标
target: 跳转到的目标页
<base target=_blank><!-- 在空白页打开 -->
<base target=_parent> <!-- 在当前页的上一页(父类)打开 -->
<base target=_search> <!-- 在浏览器地址栏打开-->
<base target=_self> <!-- 在当前页打开-->
<base target=_top> <!-- 在最初(首页)页打开 -->
这个问题解决了,但是新的问题又来了,就是文件不可以下载。所以我有没有那种方法既可以在本页打开,又可以提供下载呢?想到了iframe框架。我们可以设置一个看不见的iframe框架,然后target=iframName就可以解决了。
[html] view
plain copy
print?
<iframe id="download" name="download" height="0px" width="0px"></iframe>
<base target="download">
这<base...>位于<head></head>之间
相关文章推荐
- 深入浅出 JavaScript 中的 this
- 在IIS7.5打开网页的时候,提示: HTTP 错误 500.0 - Internal Server Error 调用 LoadLibraryEx 失败,在 ISAPI 筛选器 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\\aspnet_filter.dll" 上。解决方法
- Score
- html标签属性大全
- showModelDialog的使用(一)
- 复利计算再升级——连接数据库
- 关于序列化
- in与exist,not in与not exsit的区别
- php中的CURL库
- Hadoop、Spark、HBase与Redis的适用性见解
- 手势事件与TableViewCell点击事件冲突
- HDU 2544最短路 dijikstra算法
- Ajax解决浏览器的缓存问题
- JavaScript快速排序算法QuickSort实现
- 使用C++ 标准库 stringstream实现各种数据类型之间的转换
- requireJs和angularJs集成
- 如何将MDI窗口中的RibbonPage合并到父窗口的Ribbon中
- Linux下进程间通信之共享内存
- leetcode 27 Romove Element
- 第一周-Coursera/Stanford机器学习课程学习笔记-单变量线性回归