微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

xp:fileUpload 的提交按钮需要完整更新吗?

如何解决xp:fileUpload 的提交按钮需要完整更新吗?

在我的 XPage 上,我有一个 xp:fileUpload 组件和一个 xp:button 来提交文件。但是似乎我需要设置完整更新选项,否则文件不会发送到服务器?因此,在 Chrome 中,我仍然专注于放置表单,但在 Edge 中,焦点设置在表单的顶部。

是否可以只设置部分刷新?

按钮代码如下所示:

<xp:button id="btnSaveFile" styleClass="btnSaveFile">
    <xp:this.attrs>
        <xp:attr name="data-placement" value="bottom" />
        <xp:attr name="data-toggle" value="tooltip" />
    </xp:this.attrs>
    <xp:this.title><![CDATA[#{javascript:xptI18NBean.getValue("matter.gen_Attachment_Help")}]]></xp:this.title>
    <xp:this.value><![CDATA[#{javascript:xptI18NBean.getValue("matter.attachment_upload")}]]></xp:this.value>
    <i class="fa fa-upload" aria-hidden="true" />
    &#160;
    <xp:eventHandler event="onclick" submit="true" refreshMode="complete" disableValidators="true"
        onError="console.log('onerror')" onStart="console.log('onstart')">
        <xp:this.action><![CDATA[#{javascript:attachmentBean.save(compositeData.ref,compositeData.key,compositeData.copyFields);
attachmentBean.loadList(ref,key);
viewScope.put("attachments" + ref + key,attachmentBean.getAttachments());}]]></xp:this.action>
        
    </xp:eventHandler>
</xp:button>

这是我调用的java代码

public void save(String parentId,String type,String fields,IUploadedFile iUploadedFile) {
    utils.printToConsole(this.getClass().getSimpleName().toString() + " - save()");
    try {
        if (null != iUploadedFile) {
            Database db = sess.getDatabase(server,filePath + fileName);
            if (db.isopen()){
                String fieldName = null;
                //get the uploaded file
                //IUploadedFile iUploadedFile = uploadedFile.getUploadedFile();

                //get the original filename
                String tempClientFile = iUploadedFile.getClientFileName();
                utils.printToConsole("# iUploadedFile - getClientFileName: " + iUploadedFile.getClientFileName());
                utils.printToConsole("# iUploadedFile - getServerFileName:  " + iUploadedFile.getServerFileName());
                utils.printToConsole("# iUploadedFile - getContentType:  " + iUploadedFile.getContentType());
                utils.printToConsole("# iUploadedFile - getContentLength:  " + iUploadedFile.getContentLength());

                //get the server file (with a cryptic filename)
                File tempFile = iUploadedFile.getServerFile();                  
                utils.printToConsole("# tempFile exist: " + tempFile.exists()); 
                if(!tempFile.exists()){
                    XspOpenLogUtil.logErrorEx(null,"tempFile " + tempFile + " does not exist for user " + sess.getUserName(),Level.SEVERE,null);
                }
                utils.printToConsole("# tempFile canread: " + tempFile.canRead());
                if(!tempFile.canRead()){
                    XspOpenLogUtil.logErrorEx(null,"tempFile " + tempFile + " not readable for user " + sess.getUserName(),null);
                }
                utils.printToConsole("# tempFile canwrite: " + tempFile.canWrite());
                if(!tempFile.canWrite()){
                    XspOpenLogUtil.logErrorEx(null,"tempFile " + tempFile + " not writable for user " + sess.getUserName(),null);
                }
                utils.printToConsole("# tempFile ishidden: " + tempFile.isHidden());
                if(tempFile.isHidden()){
                    XspOpenLogUtil.logErrorEx(null,"tempFile " + tempFile + " is hidden for user " + sess.getUserName(),null);
                }
                
                File correctedFile = new File( tempFile.getParentFile().getAbsolutePath() + File.separator + tempClientFile );
                utils.printToConsole("# correctedFile getName: " + correctedFile.getName());
                utils.printToConsole("# correctedFile getPath: " + correctedFile.getPath());
                utils.printToConsole("# correctedFile getAbsolutePath: " + correctedFile.getAbsolutePath());
                utils.printToConsole("# correctedFile getCanonicalPath: " + correctedFile.getCanonicalPath());

                boolean success = false;                    
                try{
                    success = tempFile.renameto(correctedFile);
                } catch (Exception e){
                    XspOpenLogUtil.logErrorEx(e,"Could not rename temp file " + tempFile + " to " + correctedFile + " for user " + sess.getUserName(),null);
                }                   
                
                if (success) {
                    //do whatever you want here with correctedFile
                    String borreSummary = null;
                    Document parentDoc = db.getDocumentByUNID(parentId);
                    if (null != parentDoc){
                        if(type.trim().equalsIgnoreCase(parentId.trim())) {
                            String newParentID = parentDoc.getItemValueString("parentId");                              
                            parentDoc = db.getDocumentByUNID(newParentID);
                        }
                        
                        fieldName = "fldKundSammandrag";
                        if (parentDoc.hasItem(fieldName)){
                            borreSummary = parentDoc.getItemValueString(fieldName);
                        }
                    }                   

                    Document doc = db.createDocument();
                    doc.appendItemValue("Form","fa_Attachment");
                    doc.appendItemValue("parentId",parentId);
                    doc.appendItemValue("type",type);

                    Name origAuthor = sess.createName(sess.getEffectiveUserName());

                    //get the groups the user belongs to
                    Vector<Name> groupNameList = sess.getUserGroupNameList();
                    Vector<String> groups = new Vector<String>();
                    for (int i = 0; i < groupNameList.size(); i++) {
                        Name groupName = groupNameList.elementAt(i);
                        groups.add(groupName.getAbbreviated()); 
                    }

                    Vector<String> vecAuthors = new Vector<String>(4);
                    vecAuthors.add("[Admin]");
                    vecAuthors.add("[SuperAdmin]");
                    vecAuthors.add("[SuperDuper]");
                    if(groups.contains("KKOM_OpAdmin")){
                        vecAuthors.add("KKOM_OpAdmin"); 
                    }
                    if(groups.contains("KKOM_KYC Center")){
                        vecAuthors.add("KKOM_KYC Center");  
                    }
                    if (null!=parentDoc) {
                        if (parentDoc.hasItem("fldKontorChefFName")) {
                            if(!parentDoc.getItemValueString("fldKontorChefFName").trim().equalsIgnoreCase("")){
                                vecAuthors.add(parentDoc.getItemValueString("fldKontorChefFName").trim());
                            }
                        }
                        if (parentDoc.hasItem("fldKundsvarig")) {
                            if(!parentDoc.getItemValueString("fldKundsvarig").trim().equalsIgnoreCase("")){
                                vecAuthors.add(parentDoc.getItemValueString("fldKundsvarig").trim());
                            }
                        }                                   
                    }
                    vecAuthors.add(origAuthor.getCanonical());

                    TreeSet<String> uniqueAuthors = new TreeSet<String>(vecAuthors);
                    vecAuthors = new Vector<String>(uniqueAuthors);

                    Item authorRole = doc.replaceItemValue("Authors",vecAuthors);
                    authorRole.setAuthors(true);
                    
                    if (null!=parentDoc) {
                        if (parentDoc.hasItem("Officers")){
                            Item item = parentDoc.getFirstItem("Officers");
                            doc.copyItem(item,null);
                        }
                    }
                    
                    if (null!=parentDoc) {
                        if (parentDoc.hasItem("Reciters")){
                            Item item = parentDoc.getFirstItem("Reciters");
                            doc.copyItem(item,null);
                        }
                    }
                    
                    if (null!=parentDoc) {
                        if (parentDoc.hasItem("Readers")){
                            Item item = parentDoc.getFirstItem("Readers");
                            doc.copyItem(item,null);
                        }
                    }
                    
                    if(null != borreSummary){
                        if (parentDoc.hasItem("Support")){
                            Item item = parentDoc.getFirstItem("Support");
                            doc.copyItem(item,null);
                        }       
                    }

                    RichTextItem rtFiles = doc.createRichTextItem("files");               
                    rtFiles.embedobject(lotus.domino.Embeddedobject.EMbed_ATTACHMENT,"",correctedFile.getAbsolutePath(),null);
                    try {
                        JsonJavaObject objJson = (JsonJavaObject) JsonParser.fromJson(JsonJavaFactory.instanceEx,fields);
                        if (null != objJson){
                            ArrayList<JsonJavaObject> listFields = (ArrayList<JsonJavaObject>) objJson.get("fields");       
                            if(null != listFields){
                                
                                for(int i=0; i<listFields.size(); i++){
                                    JsonJavaObject fieldItem = listFields.get(i);
                                    
                                    String itemRef = (String) fieldItem.get("name");
                                    if (parentDoc.hasItem(itemRef)){
                                        Item item = parentDoc.getFirstItem(itemRef);
                                        doc.copyItem(item,null);
                                        Item docItem = doc.getFirstItem(itemRef);
                                        String fieldType = (String) fieldItem.get("type");
                                        if (fieldType.equals("Names")){
                                            docItem.setNames(true);
                                        }
                                        if (fieldType.equals("Authors")){
                                            docItem.setAuthors(true);
                                        }
                                        if (fieldType.equals("Readers")){
                                            docItem.setReaders(true);
                                        }
                                        if (fieldType.equals("Text")){
                                            docItem.setSummary(true);
                                        }
                                    }
                                }
                            }
                        }
                    } catch (JsonException e1) {
                        XspOpenLogUtil.logErrorEx(e1,JSFUtil.getXSPContext().getUrl().toString(),null);
                    }
                    
                    doc.save();
                    utils.printToConsole("# attachment doc saved");
                    
                    //new!
                    if(utils.validValueInPropertyFile(propDataSources,"vw_attach_unid_type")){
                        View vw = db.getView(propDataSources.getProperty("vw_attach_unid_type"));
                        if (null != vw){    
                            vw.refresh();   
                        }
                    }
                    
                    //set a viewscope with doc UNID of latest uploaded file for whatever purpose
                    ExtLibUtil.getViewScope().put("attachmentUnid",doc.getUniversalID());                      

                    //if we're done: rename it back to the original filename,so it gets cleaned up by the server
                    utils.printToConsole("# correctedFile exists: " + correctedFile.exists());
                    if(!correctedFile.exists()){
                        XspOpenLogUtil.logErrorEx(null,"correctedFile " + correctedFile + " does not exist for user " + sess.getUserName(),null);
                    }
                    utils.printToConsole("# correctedFile canread: " + correctedFile.canRead());
                    if(!correctedFile.canRead()){
                        XspOpenLogUtil.logErrorEx(null,"correctedFile " + correctedFile + " is not readable for user " + sess.getUserName(),null);
                    }
                    utils.printToConsole("# correctedFile canwrite: " + correctedFile.canWrite());
                    if(!correctedFile.canWrite()){
                        XspOpenLogUtil.logErrorEx(null,"correctedFile " + correctedFile + " is not writablee for user " + sess.getUserName(),null);
                    }
                    if(correctedFile.isHidden()){
                        XspOpenLogUtil.logErrorEx(null,"correctedFile " + correctedFile + " is hidden for user " + sess.getUserName(),null);
                    }
                    utils.printToConsole("# correctedFile ishidden: " + correctedFile.isHidden());
                    try{
                       correctedFile.renameto(tempFile);
                    } catch (Exception e){
                        String msg = null;
                        if(utils.validValueInPropertyFile(propStrings,"attachment_growl_file_upload_error")){
                            msg = propStrings.getProperty("attachment_growl_file_upload_error");
                        }
                        MultiGrowlMessages.createGrowlMessage(formatPropertyValuetoUTF(msg),"warning");                            
                        XspOpenLogUtil.logErrorEx(e,"Could not rename correctedFile file " + correctedFile + " to " + correctedFile + " for user " + sess.getUserName(),null);
                    }

                    rtFiles.recycle();
                    
                    String msg = null;
                    if(utils.validValueInPropertyFile(propStrings,"attachment_growl_file")){
                        msg = propStrings.getProperty("attachment_growl_file");
                    }                       
                    MultiGrowlMessages.createGrowlMessage(formatPropertyValuetoUTF(msg) + iUploadedFile.getClientFileName(),"success");
                    
                    if(null != tempFile && tempFile.canWrite()){
                        tempFile.delete();
                    }
                    if(null != correctedFile && correctedFile.canWrite()){
                        correctedFile.delete();
                    }
                }
                else{
                    String msg = null;
                    if(utils.validValueInPropertyFile(propStrings,"attachment_growl_file_upload_error")){
                        msg = propStrings.getProperty("attachment_growl_file_upload_error");
                    }                       
                    MultiGrowlMessages.createGrowlMessage(msg,"warning");                      
                    XspOpenLogUtil.logErrorEx(null,"Could not rename file " + correctedFile + " to " + tempFile + " for user " + sess.getUserName(),null);
                }
            }        
        }else{
            String msg = null;
            if(utils.validValueInPropertyFile(propStrings,"attachment_growl_no_file")){
                msg = propStrings.getProperty("attachment_growl_no_file");
            }               
            MultiGrowlMessages.createGrowlMessage(formatPropertyValuetoUTF(msg),"error");
        }
    } catch (Exception e) {
        String msg = null;
        if(utils.validValueInPropertyFile(propStrings,"attachment_growl_file_upload_error")){
            msg = propStrings.getProperty("attachment_growl_file_upload_error");
        }           
        MultiGrowlMessages.createGrowlMessage(formatPropertyValuetoUTF(msg),"warning");    
        XspOpenLogUtil.logErrorEx(e,null);
    } 
}

我尝试了其他选项(部分更新、不更新、不提交、设置部分执行),以便我可以在事件处理程序上为 oncomplete 设置一个操作:

<xp:this.onComplete><![CDATA[console.log("completed remove");
XSP.partialRefreshGet("#{id:pnlDocuments}");
var el = XSP.getElementById('#{id:pnlFiles}');
var ef = dojo.byId(el);
ef.focus();]]></xp:this.onComplete>

注意:此代码驻留在我在表单上重复使用的自定义控件上。它在 chrome 中运行良好,但(大部分)在边缘/互联网浏览器中不起作用。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。