0 Replies Latest reply on Sep 14, 2011 4:04 PM by cobar

    rich:fileUpload Viewing uploaded file content

    cobar

      I need help as I am out of ideas and solutions to an ajaxified fileupload solution.

       

      I have successfully implemented the rich:fileUpload simular to the demo. The exception is that I am saving the uploaded file in Oracle as a blob and I am only uploading .xls and .doc files. My issue now is trying to view the xls and doc files.

       

      I am taking the basic mime type streaming approach to viewing the uploaded file from an actionListener.:

       

      {code}

      <rich:simpleTogglePanel id="evntAttchTog" switchType="client" label="Attachments" headerClass="dataTableTop" styleClass="innerContainers" style="width:100%;margin-bottom:5px;">

           <a4j:region id="attchRgn" renderRegionOnly="true">

                <h:panelGrid id="evntAttchGrd" columns="1">

                     <h:panelGrid id="attchFileGrd" columns="2" columnClasses="fileUploadCols">

                          <h:dataTable id="attchTbl" styleClass="dataTable" style="width: 450px;" columnClasses="dataTableColumn" rowClasses="dataTableInnerEven, dataTableInnerOdd"

                               value="#{managedBean.selectedSchedule.attachments}" var="attch" binding="#{managedBean.attachmentTbl}">

       

                               <h:column>

                                    <h:commandLink id="attchViewBtn" immediate="false" actionListener="#{managedBean.showAttachment}">

                                         <f:attribute name="attachNdx" value="#{managedBean.attachmentTbl.rowIndex}"/>

                                         <h:graphicImage id="attchVwImg" url="/images/binocular.png" alt="View attachment" style="border:0;"/>

                                    </h:commandLink>

       

                                    <a4j:commandButton id="attchDelBtn" image="/images/Trash.gif" style="margin-left:5px;" reRender="attchTbl,attchMsg,attchFU" immediate="false" actionListener="#{managedBean.deleteAttachment}">

                                         <f:attribute name="attachNdx" value="#{managedBean.attachmentTbl.rowIndex}"/>

                                    </a4j:commandButton>

                               </h:column>

                               <h:column>

                                    <!-- PhaseListener solution -->

                                    <h:outputLink value="schedule.xhtml">

                                         <f:param name="viewAttch" value="t"/>

                                         <f:param name="attachId" value="#{attach.id}"/>

                                         <h:outputLabel value="#{attch.fileName}"/>

                                    </h:outputLink>

                               </h:column>

                               <h:column> <h:outputLabel id="attchLud" value="#{attch.lastUpdtId}"/> </h:column>

                               <h:column> <h:outputText id="rmkLut" value="#{attch.lastUpdtTmstp}"> <f:convertDateTime pattern="#{bundle.shortTimeFormat}"/></h:outputText></h:column>

                          <h:dataTable>

       

                          <rich:fileUpload id="attchFU" immediateUpload="true" style="width:500px;" listHeight="70px"

                               sizeErrorLabel="File is to large to upload." sizeErrorLabelClass="errorSmall"

                               maxFilesQuantity="#{managedBean.selectedSchedule.availableAttachments}"

                               fileUploadListener="#{managedBean.uploadAttachment}"

                               acceptedTypes="#{managedBean.availableContentTypes}">

                         

                               <a4j:support id="attchFuSprt" event="onuploadcomplete" reRender="attchTbl,attchMsg,attchFU"/>

                          </rich:fileUpload>

       

                     </h:panelGrid>

                </h:panelGrid>

           <a4j:region>

       

      </rich:simpleTogglePanel>

      {code}

       

       

      {code}

      public void showAttachment() {

           ServletOutputStream out = null;

           int len;

           try {

                Integer rowNdx = (Integer) ae.getComponent().getAttributes().get("attachNdx");

                Attachment attch = getSelectedSchedule().getAttachments().get(rowNdx.intValue());

       

                FacesContext fc = FacesContext.getCurrentInstance();

                HttpServletResponse response = fc != null ? (HttpServletResponse) fc.getExternalContext().getResponse() : null;

                response.setContentType(attch.getContentType());

                response.setHeader("Expires", "0");

                response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");

                response.setHeader("Pragma", "public");

                response.setHeader("Content-disposition", "attachment; filename=" + attch.getFileName());

       

                out = response.getOutputStream();

                Blob attchContent = null;

                if (attch.getAttachment() == null) {

                     attchContent = sessionBean.retrieveAttachmentContent(attch.getId());

                     attch.setAttachment(attchContent);

                } else {

                     attchContent = attch.getAttachment();

                }

       

                InputStream iStream = attchContent.getBinaryStream();

                byte[] bBuffer = new byte[1024];

                len = 0;

                while ((len = iStream.read(bBuffer)) > 0) {

                     out.write(bBuffer, 0, len);

                }

       

                fc.responseComplete();

       

           } catch(Throwable t) {

                logger.log(t.getMessage());

           } finally {

                try{  out.close(); }catch(Throwable t){ }

           }

      }

       

      {code}

       

      Any type of non-ajax event fails with duplicate id for which the id does not exist on the viewed source.

       

      Any type of ajax event fails with an empty page containing no html when no action is specified. Adding an action of the same page results in the duplicate id issue.

       

      Using a h:commandLink and a phase listener I get "java.lang.IllegalStateException: Servlet response already use stream, Writer not possible" which makes since since the action is the same page.

       

      I have successfully implemented the straight non-ajax apache upload solution but it was rejected because the form submission results in having to scroll back down the page to get to the results of the file upload.