EAS WEB附件下载实现

2024-01-09 00:09:05

方式一:请求后台代码方式

前端js:

	    //调用后台,封装附件数据
          waf.doPost({
                action:"AttachmentListUIService",
                data:{
                    methodName: "getFiles",
                    ids: ids //附件id数组
                },
                success:function(result){
                    for(var j=0;j<result.length;j++){
                        var dataMap = result[j];
                        //文件字节流
                        var fileByte = dataMap.fileByte;
                        //字节流数组转换
                        var arr = new Uint8Array(fileByte.length);
                        for (var i = 0;i<fileByte.length;i++){
                             arr[i] = fileByte[i];
                        }
                        //文件名
                        var fileName = dataMap.fileName;
                        var url = window.URL.createObjectURL(new Blob([arr]));
                        var link = document.createElement('a');
                        link.style.display = 'none';
                        link.href = url;
                        link.setAttribute('download', fileName);
                        document.body.appendChild(link);
                        link.click();
                        document.body.removeChild(link);
                   }
                }
            });

后台代码:

   private HashSet getFilesObj(KDActionEvent event) throws EASBizException, BOSException {
        HashSet dataSet = new HashSet();
        //所选id封装
        String[] ids = (String[])event.getReqeustContext().getHttpServletRequest().getParameterMap().get("ids[]");
        HashSet<String> idSet = new HashSet<String>(Arrays.asList(ids));
        //查询所选id附件
        EntityViewInfo view = new EntityViewInfo();
        FilterInfo filterInfo = new FilterInfo();
        filterInfo.getFilterItems().add(new FilterItemInfo("id",idSet,CompareType.INCLUDE));
        view.setFilter(filterInfo);
        AttachmentCollection coll = AttachmentFactory.getRemoteInstance().getAttachmentCollection(view);
        //遍历附件集合
        for(int i=0;i<coll.size();i++){
            //存储当前文件信息
            HashMap<String,Object> dataMap = new HashMap<String,Object>();
            AttachmentInfo attachmentInfo = coll.get(i);
            attachmentInfo = AttachmentFactory.getRemoteInstance().getAttachmentInfo(new ObjectUuidPK(attachmentInfo.getId()));
            //文件名称
            String fileName = attachmentInfo.getName();
            //文件类型
            String simpleName = attachmentInfo.getSimpleName();
            //文件数据
            byte[] fileBtye = null;
            //文件存储方式
            AttachmentStorageTypeEnum storageType = attachmentInfo.getStorageType();
            if(AttachmentStorageTypeEnum.FTP == storageType){
                //ftp服务器方式
                FtpConfigInfo ftp = attachmentInfo.getFtp();
                ftp = FtpConfigFactory.getRemoteInstance().getFtpConfigInfo(new ObjectUuidPK(ftp.getId()));
                String remotePath = attachmentInfo.getRemotePath();
                byte[] file = FtpHandleFacadeFactory.getRemoteInstance().download(ftp, remotePath);
            }else if(AttachmentStorageTypeEnum.DATABASE == storageType){
                //数据库方式
                fileBtye = attachmentInfo.getFile();
            }
            //封装数据
            dataMap.put("fileName", fileName+"."+simpleName);
            dataMap.put("fileByte", fileBtye);
            dataSet.add(dataMap);
        }
        return dataSet;
    }

方式二:标准web链接下载方式

        downloadFiles: function (e) {
            //附件id
            var ids = waf("#queryGrid").wafGrid("getSelectedRows");
            if (ids == null || ids == "") {
                _self.showWarning("请选择需要下载的附件。");
                return false;
            }
            //批量下载
            for (var i = 0; i < ids.length; i++) {
                //文件名
                var fileName = waf("#queryGrid").wafGrid("getCell", ids[i], "name");
                var fileType = waf("#queryGrid").wafGrid("getCell", ids[i], "simpleName");
                var link = document.createElement('a');
                link.style.display = 'none';
                link.href = "/easweb/webviews/webframework/webcom/attachment/download.jsp?bosID=" + ids[i];
                link.setAttribute('data_url', ids[i]);
                link.setAttribute('target', "_blank");
                link.setAttribute('download', fileName + "." + fileType);
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
            }
        }

文章来源:https://blog.csdn.net/ZhaZha__Hui/article/details/135414836
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。