4 Replies Latest reply: Aug 9, 2012 8:49 AM by 856652 RSS

    Prevent file upload when clicking on cancel button

    Juraj.K-Oracle
      Hello all,

      I have a problem with cancelling a form containing file upload field. In short, the upload happens even in case I click on Cancel button. What's the way to prevent "submitting" the form, but letting the server side continue navigating?
      In long, I have such form:
      <?xml version='1.0' encoding='UTF-8'?>
      <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
        <jsp:directive.page contentType="text/html;charset=UTF-8"/>
        <f:view>
          <af:document id="d1" title="File upload">
            <af:form id="f1" usesUpload="true">
              <af:inputFile label="Choose file: " id="if1" required="true"
                            showRequired="false"
                            requiredMessageDetail="You have to choose the file."/>
              <af:commandButton text="Upload" id="cb1" action="submit"/>
              <af:commandButton text="Cancel" id="cb2" immediate="true"
                                action="cancel"/>
            </af:form>
          </af:document>
        </f:view>
      </jsp:root>
      with very simple unbounded taskflow - action <font face="courier">submit</font> goes to one page (pageFileUploaded), action <font face="courier">cancel</font> to another page (pageCancel).

      The problem is that even I click on Cancel the file choosen is being uploaded and just then the pageCancel is shown, which in case of big files or slow connections is not acceptable. How to prevent this? How to show the pageCancel immediately after clicking Cancel button?

      Thank you in advance.

      Regards,
      Rox

      P.S.: Using JDeveloper 11.1.1.5.0.
        • 1. Re: Prevent file upload when clicking on cancel button
          Frank Nimphius-Oracle
          Hi,

          this indeed is an odd behavior. However, surrounding the form parts that should be updated when form submit is pressed with an af:subform tag solves the issue
          <af:subform id="s1">
                    <af:inputFile label="Choose file" id="fileup" clientComponent="true"
                                  value="#{FileUploadBean.file}"  valueChangeListener="#{FileUploadBean.onFileUpload}">
                    <af:clientListener method="preventUserInput" type="valueChange"/>
                  </af:inputFile>
                  <af:commandButton text="Submit" id="cb1" partialSubmit="true"
                                    action="#{FileUploadBean.onInputFormSubmit}"/>
             </af:subform>
          Actually <af:commandButton text="cancel" id="cb2" immediate="true" partialSubmit="true"/> helped too.

          Frank
          • 2. Re: Prevent file upload when clicking on cancel button
            Juraj.K-Oracle
            Hi Frank,

            I'm sorry, but I can't make it working... I've tried both proposed solutions (actually, I was following your blog entry on this topic), but it still uploads the file even when clicking on Cancel button. I've set UPLOAD_MAX_MEMORY, UPLOAD_MAX_DISK_SPACE, UPLOAD_TEMP_DIR parameters to be able to upload big files and then I watched the temp directory after clicking the Cancel button - the loading of next page takes a while and the file grows up in temp dir...

            I'm using JDeveloper 11.1.1.5 (PS4).

            If you want to review my code, I can send it you, or you can export from SVN at http://subversion.assembla.com/svn/synchron/jdeveloper/adf/FileUploadCancel - I will keep it public for a while.

            Thank you.

            Best regards,
            Rox
            • 3. Re: Prevent file upload when clicking on cancel button
              856652
              Hi,
              I'm using JDeveloper 11.1.1.5.
              The proposed solution with partialSubmit="true" isn't working for me too. Also I'm not able to use the subform because of layouting issues.
              So anyone has some other ideas how to solve this?
              Best regards,
              Markus
              • 4. Re: Prevent file upload when clicking on cancel button
                856652
                Ok, I found a hack/workaround. It's not my preferred solution, but it works:
                <?xml version='1.0' encoding='UTF-8'?>
                <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
                          xmlns:f="http://java.sun.com/jsf/core"
                          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
                  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
                  <f:view>
                    <af:document id="d1" title="Upload form.">
                      <af:resource type="javascript">
                        function resetInputFile()
                        {
                            var fileInput = document.getElementById('if1::content');
                            if (fileInput != null)
                            {
                                fileInput.value = null;
                            }
                        }
                      </af:resource>
                      <af:form id="f1" usesUpload="true">
                        <af:inputFile label="Choose file:" id="if1"
                                      value="#{FileUploadBean.file}" clientComponent="true"/>
                        <af:commandButton text="Upload" id="cb1"
                                          action="#{FileUploadBean.onFileUploadSubmit}"/>
                        <af:commandButton text="Cancel" id="cb2" action="cancel"
                                          partialSubmit="true" immediate="true">
                          <af:clientListener method="resetInputFile" type="action"/>
                        </af:commandButton>
                      </af:form>
                    </af:document>
                  </f:view>
                </jsp:root>
                I hope someone out there has a better solution for this.
                Regards,
                Markus