下载功能---文件关键代码

1.前端

1.1:下载按钮

<a onclick="download_attach()" class="easyui-linkbutton"
style="width: 70px; height: 35px; margin-left: 30px; background-color: #8E8E8E">下载</a>

1.2:将要下载的文件,id,名字,路径传参

//下载文件
function download_attach() {
var selectRow = $$.getSingleSelectRow(listId, "请选择你要下载的文件!");
var FormUrl = "/wcm/actions/document_library/download_attach.do?actionId=mswcm_document_library_download_attach";
if (selectRow) {
window.location.href = FormUrl + '&attach_id=' + selectRow.attach_id +'&attach_title='+selectRow.attach_title +'&attach_path='+selectRow.attach_path ;
}
}

2.后端

/**
* 下载附件
*
* @throws UnsupportedEncodingException
*/
@RequestMapping(value = "/download_attach")
public CIPResponseMsg download_attach(HttpServletRequest request,
HttpServletResponse response) throws UnsupportedEncodingException {
CIPResponseMsg msg = new CIPResponseMsg();
// 锁定list要下载的附件
String attach_id = request.getParameter("attach_id");
String attach_title = request.getParameter("attach_title");
attach_title = new String(attach_title.getBytes("iso8859-1"), "utf-8");
String attach_path = request.getParameter("attach_path");
try {
boolean xAuth = true;
if (xAuth) {
DownloadUtil downloadUtil = new DownloadUtil();
// 锁定原始附件的路径+文件名称
String fileName = attach_path;
File file = new File(fileName);
downloadUtil.prototypeDownload(file, attach_title, response,false);      //关键性代码, file--路径,attach_title--名字
} else {
msg.errorCode = CIPErrorCode.ERROR_INVALID_AUTHORIZATION.code;
msg.msg = CIPErrorCode.ERROR_INVALID_AUTHORIZATION.name;
}
} catch (CIPServiceException e) {
CIPErrorCode error = e.getErrorCode();
msg.errorCode = error.code;
msg.msg = error.name;
} catch (CIPDaoException e) {
CIPErrorCode error = e.getErrorCode();
msg.errorCode = error.code;
msg.msg = error.name;
} catch (CIPRuntimeException e) {
CIPErrorCode error = e.getErrorCode();
msg.errorCode = error.code;
msg.msg = error.name;
}

return msg;
}

posted @ 2018-08-31 11:26  up-zyn  阅读(420)  评论(0编辑  收藏  举报