//TODO 事件委托,下载附件按钮
$(".taskRows").on("click",".downloadAttach",function(){
var attachIndex= $(this).attr("id")//获取当前点击的元素ID
attachIndex = attachIndex.charAt(attachIndex.length-1);
var attach_name= $.trim($("#attach_name"+attachIndex).text());
alert(attach_name);//根据附件名下载附件
if(attach_name!=null &&attach_name!=undefined)
{//$("#downloadFrame"+attachIndex).attr("src",baseCtx+"/wpgl/downloadAttachFile.action?attachName="+attachName);
$("#downloadFrame"+attachIndex).attr("src",baseCtx+"/wpgl/downloadAttachFile.action?Id="+Id+"&attachName="+encodeURI(encodeURI(attachName)));
}
});//action 后台一些主要的包和类
importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.OutputStream;importjava.io.UnsupportedEncodingException;importjava.util.ArrayList;importjava.util.Date;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjava.net.URLDecoder;importjava.net.URLEncoder;/*** 下载文件
*@return*@throwsUnsupportedEncodingException*/@Action(value="downloadAttachFile")publicString downloadAttachFile(){//从前台传过来的附件名字:格式是,“2006#系统测试.docx格式"
String downloadName =wpyId+"#"+attachName;//传入的数据
System.out.println(downloadName);try{
OutputStream os= this.response.getOutputStream();//OutputStream os = this.getResponse().getOutputStream();
if (Util.isIE(this.request)) //判断客户端是否为IE
{//编码
downloadName = URLEncoder.encode(downloadName, "UTF-8");
}else{
downloadName= new String(downloadName.getBytes("UTF-8"),"iso-8859-1");
}//设置让浏览器弹出下载对话框的Header
this.response.setContentType("application/x-download");this.response.addHeader("Content-Disposition","attachment;filename=\"" + downloadName + "\"");this.response.flushBuffer();//将前台的数据经过包装后(加了头数据,并经过编码)需要解码
String downloadName1 =URLDecoder.decode(downloadName, "UTF-8");
System.out.println(downloadName1);//解码的结果输出
String bgFile=realPath + File.separator +downloadName1;
FileInputStream fis= newFileInputStream(bgFile);
Util.copyStrem(fis, os);
fis.close();
os.close();
System.out.println("==========成功了!!===========");
}catch(IOException e) {
e.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因
System.out.println("==========出错了!!===========");
}returnNONE;
}