0

我有一个用于将文件上传到页面的自定义控件。它工作得很好,但只有当它不在xp.this.rendered属性内时。如果是这样,它会给我带来非常意想不到的结果。这是我使用的 div 的代码:

        <div style="height:0px;overflow:hidden">
            <input type="file"
                id="${javascript:compositeData.ID+'_files_input'}"
                onchange="#{javascript:
      var currentCustomID = compositeData.ID; 

      var filesInput = '\'' + currentCustomID + '_files_input' + '\'';
      var filesUpload = '\'' + currentCustomID + '_files_upload' + '\'';
      var filesButton = '\'' +  currentCustomID + '_files_button' + '\'';
      var filesProgress = '\'' +  currentCustomID + '_files_progress' + '\'';

      return 'files_onchange(' + filesInput + ',' + filesUpload + ',' + filesButton + ',' + filesProgress + ')';
      }"
                multiple="true" uploadOnSelect="true" name="uploadedfile" />

            <xp:fileUpload
                id="${javascript:compositeData.ID+'_files_upload'}"
                useUploadname="true">
                <xp:this.value><![CDATA[#{doc_source[compositeData.FieldName]}]]></xp:this.value>
            </xp:fileUpload>

<xp:button value="Refresh"
        id="${javascript:compositeData.ID+'_files_button'}">
        <xp:eventHandler event="onclick" submit="true"
          refreshMode="partial"
          refreshId="${javascript:compositeData.ID+'refresh'}"
          disableValidators="true">
          <xp:this.action>


            <xp:actionGroup>
              <xp:actionGroup>

              <xp:executeScript
                script="#{javascript:print('But server side executes only without RENDERED')}">
              </xp:executeScript>

                <xp:saveDocument></xp:saveDocument>
                <xp:executeScript>
                  <xp:this.script><![CDATA[#{javascript:
if (compositeData.postUpload!=null)
{
compositeData.postUpload.getScript().invoke(facesContext, null)
}
//print("сука");
}]]></xp:this.script>
                </xp:executeScript>
              </xp:actionGroup>

            </xp:actionGroup>
          </xp:this.action>

          <xp:this.script>
            <xp:executeClientScript
              script="console.log('CSJS works so well')">
            </xp:executeClientScript>
          </xp:this.script>
          </xp:eventHandler>
      </xp:button>
        </div>

在上面的代码中有一个刷新按钮,它应该保存文档,以便文档保存它刚刚收到的文件。

此外,还有发送带有表单数据的 XHR 请求的代码:

function files_onchange(filesInput, filesUpload, filesButton, filesProgress) 
{
    var urfiles = document.getElementById(filesInput).files;
    files_upload(filesInput, filesUpload, urfiles, 0, filesButton, filesProgress);
}           

function files_upload(filesInput, uploadID, files, counter, refreshID, filesProgress)
{

    var url = window.location.href;

    var formData = new FormData();
    var file = null;
    var form = XSP.findForm(filesInput);
    if (!files) return;
    var numberOfFiles = files.length;
    file = files[counter];
    var max = files.length;
    if (counter >= max) return;

    formData.append(document.querySelector('[id$=' + uploadID + ']').id, file);
    formData.append("$$viewid", dojo.query("input[name='$$viewid']")[0].value);
    formData.append("$$xspsubmitid", dojo.query("input[name='$$xspsubmitid']")[0].value);
    formData.append("$$xspsubmitvalue", dojo.query("input[name='$$xspsubmitvalue']")[0].value);
    formData.append("$$xspsubmitscroll", dojo.query("input[name='$$xspsubmitscroll']")[0].value);
    formData.append(form.id, form.id);


    var xhr = new XMLHttpRequest();


    /* event listners */

      xhr.upload.addEventListener("progress", function(e) 
      {
          if (e.lengthComputable)

           {
                var percentComplete = Math.round(e.loaded * 100 / e.total);
                document.getElementById(filesProgress).innerHTML = percentComplete.toString()+'%, ( '+(counter+1).toString()+' / '+numberOfFiles.toString()+' )';
           }
              else 
              {
                document.getElementById(filesProgress).innerHTML = '...';
              } 
      }, false);


      xhr.addEventListener("load", function()
      {
          counter++; 
          if (counter >= max)
          {
              document.getElementById(filesInput).value = "";

              if (refreshID) 

              {
                  document.querySelector('[id$=' + refreshID + ']').click(); // it's supposed to trigger the refresh button
              }

          } 
        else 
              { 
                files_upload(filesInput, uploadID, files, counter, refreshID, filesProgress) 
              }

      }, false);

     xhr.addEventListener("error", function(e) 
     {
         document.getElementById(filesProgress).innerHTML = "Error: "+e;

     }, false);

     xhr.addEventListener("abort", function()
     {
         document.getElementById(filesProgress).innerHTML = "Upload cancelled.";

     }, false);

      xhr.open("POST", url, true);
      xhr.send(formData);
      document.querySelector('[id$=' + uploadID + ']').value = '';
}

我不知道为什么 CSJS 代码document.querySelector('[id$=' + refreshID + ']').click()总是被执行。无论 xp.this.rendered属性如何,我总是'CSJS works so well'在浏览器控制台 But server side executes only without RENDERED中打印,而仅在没有呈现属性的情况下在服务器控制台中打印(或者如果它永久设置为 true)。背后的原因是什么?提前致谢。

4

1 回答 1

1

确保“显示错误”组件位于刷新区域并检查验证错误。这是 CSJS 运行但 SSJS 不运行的最常见原因。

于 2018-11-06T10:16:29.423 回答