概述
1.前端
1.1:下载按钮
<a οnclick="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;
}
转载于:https://www.cnblogs.com/Darkqueen/p/9564857.html
最后
以上就是有魅力香水为你收集整理的下载功能---文件关键代码的全部内容,希望文章能够帮你解决下载功能---文件关键代码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复