附件在线编辑开发案例原创
金蝶云社区-陈来珍
陈来珍
7人赞赏了该文章 2,447次浏览 未经作者许可,禁止转载编辑于2021年11月29日 15:47:35

一、需求背景

在附件面板上,添加自定义按钮-编辑,用户点击编辑时,直接以网页的形式打开附件,并修改保存到文件服务器上。

41.png

二、实现方案

42.png

1、环境准备

   使用webOffice控件实现在线编辑功能之前需求预先安装webOffice的控件软件https://dev.kingdee.com/kd/ecos/file/getById/4b426213-734c-11eb-8940-fa163ea08693,注意安装成功后使用IE浏览器打开苍穹表单。

2、附件面板添加自定义编辑按钮

自定义按钮需要在合适的时机加上,比如:

(1)       打开单据时,触发的afterBindData事件中

(2)       附件上传后事件

核心代码(详细代码看附件):

//附件面板控件
AttachmentPanel panel = this.getView().getControl("attachmentpanel");
      List<Map<String, Object>> attachmentData = panel.getAttachmentData();
      List<AttachBtnOption> btns = new ArrayList<AttachBtnOption>();
      btns.add(new AttachBtnOption("att_edit", new LocaleString("编辑")));
      panel.addAttachOperaBtn(btns);

3、创建动态表单页面

43.png

WebOffice控件建议设置成100%宽度和高度

44.png

4、弹出附件详情页面

@Override
   public void attachmentOperaClick(AttachmentOperaClickEvent e) {
      String operaKey = e.getOperaKey();
      AttachmentPanel att = (AttachmentPanel) e.getSource();
      Map<String, String> attinfo = (Map<String, String>) e.getAttachmentInfo();
      List<Map<String, Object>> atts = att.getAttachmentData();
      if (operaKey.equals("att_edit")) {
         for (Map<String, Object> map : atts) {
            String uid = (String) map.get("uid");
            if (uid.equals(attinfo.get("uid"))) {
                String url = (String) map.get("url");
                 Map<String, Object> params = new HashMap<>(16);
                if (url.contains("/tempfile/download.do?configKey")) {
                   this.getView().showMessage("请先保存单据");
                } else {
                   WebOfficeBrowserParam w = new WebOfficeBrowserParam("sunp_attinfo");
                    params.put("type", "attachment");
                    String name = (String) map.get("name");
                    params.put("name", name);
                    params.put("parentId", this.getView().getPageId());
                    String[] partUrl = url.split("/attachment/download\\.do\\?path=/");
                    // 已保存的附件地址,已经是转义过了
                    params.put("url", partUrl[1]);
                    // params.put("docurl",partUrl[1]);
                    w.setParams(params);
                    // 设置地址栏是否显示
                    // w.setHideAddressBar(true);
                    // 调用浏览器帮助类,构建地址打开参数
                    String browserUrl = WebOfficeBrowserHelper.buildUrl(w);
                    // 打开包含控件的页面地址
                    this.getView().openUrl(browserUrl);
                }
            }
         }
      }
   }

5、实现附件详情逻辑

、实现附件详情逻辑
(1)webOffice控件打开附件
WebOffice webOffice = (WebOffice) this.getControl("sunp_webofficeap");
URL=~~
webOffice.open(URL);
(2)webOffice保存附件,生成的是临时文件
WebOffice webOffice = (WebOffice) this.getControl("sunp_webofficeap");
Name=~~~
webOffice.save(name);
(3)文件服务器保存webOffice保存生成的临时文件
①获取临时文件的输入流
TempFileCache cache = CacheFactory.getCommonCacheFactory().getTempFileCache();
InputStream in = cache.getInputStream(url);
FileService service = FileServiceFactory.getAttachmentFileService();
② FileItem  fileItem = new FileItem(fileName, “附件原始地址”, in);
cache.remove(url);
//上传附件到文件服务器
turl = service.upload(fileItem);

三、实现效果

(1)点击附件编辑之前,先保存单据

45.png

(2)编辑修改附件,并保存

46.png

(3)再次打开时,就是修改后的附件,这时如果发现源文件删除不了,只能增加文字,可以选中右键选择修订

47.png

赞 7