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


    1.前端

    1.1:下载按钮

    <a onclick="download_attach()" class="easyui-linkbutton"
    style=" 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;
    }

  • 相关阅读:
    Docker入门之二镜像
    巧用Freemarker的自定义方法
    freemarker空值的多种处理方法
    ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
    Mingyang.net:自定义FreeMarkerView
    hibernate one-to-many many-to-one 双向注解
    Mingyang.net:Controller必需是public吗?
    Mingyang.net:org.springframework.context.annotation.ConflictingBeanDefinitionException
    Mingyang.net:No identifier specified for entity
    jQuery:find()及children()的区别
  • 原文地址:https://www.cnblogs.com/Darkqueen/p/9564857.html
Copyright © 2020-2023  润新知