javaajaxportletwebsphere-portal

FIle Upload with response in Portlet For IBM


What needs to be Achieved: I have been trying to Find a way to Upload file and send response with a Json if it is Successful in IBM Websphere Portlet using AJAX

How i was able to do Some Part of it: I was able to Upload the file using ActionURL but in Action i was not able to Find a way to Send response from ActionMapping

Approach needed: I know we can send response using ResourceMapping but i am not able to find a way to upload A file on Resouce Mapping

Working Code For File Upload Which needs to be updated for Recieving Response in AJAX

JSP

<form:form method="POST" 
modelAttribute="uploadForm" 
enctype="multipart/form-data">
  <input type="file" name="logo" id="logo"/>
 <button tabindex="2" type="submit" name="Next" value="Upload" id="Next" >Next</button>
</form:form>

Code:For IBM Portlet

@ActionMapping(params="file=processUploadedFile")
   public void processUploadedFileNew(@RequestParam("logo") MultipartFile file,
          final  ActionRequest request,final ActionResponse response) throws IOException {
//I was able to Handle Code here
      }

However with Above code i want to change request to AJAX and no reload of page is Desired so i need to change the Action to Resource.

Note: I am known to the technique where u can send parameters from ActionMapping to RenderMapping and send back data in ModelAndView but this is not desired as i want the response back in AJAX Success Data


Solution

  • I was able to find the solution . Please see below solution using the Jar

    import org.apache.commons.fileupload.FileUploadBase;
    import org.apache.commons.fileupload.servlet.ServletRequestContext;
    
    Code
    @ResourceMapping(value = "uploadfile")
        public void processUploadedFile(final ResourceRequest request, final ResourceResponse response, final PortletRequest portletRequest){
    
    HttpServletRequest originalHttpServletRequest = (HttpServletRequest) (com.ibm.ws.portletcontainer.portlet.PortletUtils.getHttpServletRequest(request));
            final boolean multipartContent = FileUploadBase.isMultipartContent(new ServletRequestContext(originalHttpServletRequest));
    
            if (multipartContent) {
                file= createPEService.getResourceToBeUploaded(originalHttpServletRequest,file,bytes);
            }
    }
    

    Jsp File:

    <portlet:resourceURL id="uploadfile"
        var="uploadfile" />
    
      <form class="testClass" enctype="multipart/form-data">
         </form>
    

    JS:

    isMultipart = 'multipart/form-data' === referenceForm.enctype,
     var $referenceForm = $( e.currentTarget ).parents( 'form' ),
                            referenceForm = $referenceForm.get( 0 ),
    $.ajax({
                        type: 'POST',
                        url: ${uploadfile},
                        processData: ! ( isMultipart ),
                        contentType: isMultipart ? false : 'application/x-www-form-urlencoded; charset=UTF-8',
                        data: isMultipart ? new FormData( referenceForm ) : $referenceForm.serialize(),
                        cache: false
                    }).done(function( data ) {
                   }