2.12. Upload component

In this section, you will learn how to configure the Upload service that is defined by the org.exoplatform.upload.UploadService class.

This can be configured with the following XML code:


<component>
   <type>org.exoplatform.upload.UploadService</type>
     <init-params>
       <value-param>
        <name>upload.limit.size</name>
        <description>Maximum size of the file to upload in MB</description>
        <value>10</value>
      </value-param>
    </init-params>  
  </component>

This code allows uploading files with the default size limit (10MB). The default value unit is in Megabytes.

This limitation will be used by default by all applications if no application-specific limit is set. Setting a different limitation for applications is discussed in a later section.

If the value is set to 0, the upload size is unlimited.

Using the Upload component

  1. Create an org.exoplatform.webui.form.input.UIUploadInput object type by using one of three following constructors:

    • The default constructor that allows uploading the file with the size of 10 MB.

      public UIUploadInput(String name, String bindingExpression, int limitFile)
    • This constructor allows you to customize the size limit of uploaded files by using the limitSize parameter. The default value unit is Megabytes.

      public UIUploadInput(String name, String bindingExpression,int limitFile, int limitSize)
    • This constructor allows you to customize the size limit and the value unit by using the limitSize and unit parameters respectively.

      In GateIn, you can set the value unit to Megabytes (MB), Kilobytes (KB) or Gigabytes (GB).

      public UIUploadInput(String name, String bindingExpression, int limitFile, int limitSize, UploadUnit unit)

    The following is an example using the third form:

    PortletRequestContext pcontext = (PortletRequestContext)WebuiRequestContext.getCurrentInstance();
    
          PortletPreferences portletPref = pcontext.getRequest().getPreferences();
          int limitFile = Integer.parseInt(portletPref.getValue("uploadFileLimit", "1").trim());
          int limitSize = Integer.parseInt(portletPref.getValue("uploadFileSizeLimit", "").trim());
          UploadUnit limitUnit = UploadUnit.valueOf(portletPref.getValue("uploadFileLimitUnit", "MB").trim());
    UIUploadInput uiInput = new UIUploadInput("upload", "upload", limitFile, limitSize, limitUnit);
  2. Obtain the limitation from the XML configuration by adding the following code to either portlet.xml or portlet-preferences.xml:

    
    <The number of files are uploaded -->
    <preference>
      <name>uploadFileLimit</name>
      <value>3</value>
      <read-only>false</read-only>
    </preference>
    <The size limit -->
    <preference>
      <name>uploadFileSizeLimit</name>
      <value>300</value>
      <read-only>false</read-only>
    </preference>
    <The unit limit -->
    <preference>
      <name>uploadFileLimitUnit</name>
      <value>KB</value>
      <read-only>false</read-only>
    </preference> 

    Note

    The 0 value means the upload size is unlimited, and the value unit is set to MegaBytes.

  3. Use the getUploadDataAsStream() method to get the uploaded data:

    UIUploadInput input = (UIUploadInput)uiForm.getUIInput("upload");
    
    InputStream[] inputStreams = input.getUploadDataAsStreams();
    ...

    The upload service stores a temporary file on the file system during the upload process. When the upload is finished, the service must be cleaned to:

    • Delete the temporary file.

    • Delete the classes used for the upload.

  4. Use the removeUploadResource(String uploadId) method defined in the upload service to purge the file:

    UploadService uploadService = uiForm.getApplicationComponent(UploadService.class) ;
    
    UIUploadInput uiChild = uiForm.getChild(UIFormUploadInput.class) ;
    for(String uploadId : uiChild.getUploadIds()) {
        uploadService.removeUpload(uploadId) ;
    }

    Saving the uploaded file

    Ensure the file is saved before the service is cleaned.

Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus