Class UploadItem

All Implemented Interfaces:
HasHandlers, HasBlurHandlers, HasChangedHandlers, HasChangeHandlers, HasClickHandlers, HasDoubleClickHandlers, HasEditorEnterHandlers, HasEditorExitHandlers, HasFocusHandlers, HasIconClickHandlers, HasIconKeyPressHandlers, HasItemHoverHandlers, HasKeyDownHandlers, HasKeyPressHandlers, HasKeyUpHandlers, HasPendingStatusChangedHandlers, HasPickerIconClickHandlers, HasShowContextMenuHandlers, HasTitleClickHandlers, HasTitleDoubleClickHandlers, HasTitleHoverHandlers, HasValueHoverHandlers, HasValueIconClickHandlers

public class UploadItem extends TextItem
FormItem that creates an HTML <input type="file"> control, with an interface that allows a user to pick a file from his machine to upload to the server.

NOTE: use FileItem, not UploadItem, if you are using the Smart GWT Server framework. FileItem is much easier to use and addresses all the limitations of UploadItem discussed below. See the Uploading Files overview for details.

If a form containing an UploadItem is redrawn (which may happen if other form items are shown or hidden, the form is resized, or this or other items show validation errors) then the value in the upload item is lost (because an HTML upload field may not be created with a value). For this reason, if you are building a form that combines an UploadItem with other FormItems that could trigger redraw()s, recommended practice is to place each UploadItem in a distinct DynamicForm instance and create the visual appearance of a single logical form via combining the DynamicForms in a Layout. For the same reason, do not apply validators to UploadItems: if such a validator fails, it will cause the form to be redrawn and the UploadItem's value to be lost.

NOTE: Browser-specific behaviors:

  • while getDisplayValue() can be used to retrieve the filesystem path of the uploaded file on some browsers, different browsers will return either just the file name without path or the full path. It is plausible that some browsers may switch behavior in the future to not supply this value at all. Do not rely on this value.
  • the appearance of the UploadItem is not consistent across browsers and we do not recommend trying to make it consistent or trying to apply styling to the upload control at all. It is a potential security problem if an end user is unable to reliably recognize the upload control, hence, all browsers limit what styling can be applied. Various hacks exists to get further control of styling, but it is likely these hacks will be broken by browser upgrades in the future.
See Also:
  • Constructor Details

    • UploadItem

      public UploadItem()
    • UploadItem

      public UploadItem(JavaScriptObject jsObj)
    • UploadItem

      public UploadItem(String name)
    • UploadItem

      public UploadItem(String name, String title)
  • Method Details

    • getOrCreateRef

      public static UploadItem getOrCreateRef(JavaScriptObject jsObj)
    • changeAutoChildDefaults

      public static void changeAutoChildDefaults(String autoChildName, Canvas defaults)
      Changes the defaults for Canvas AutoChildren named autoChildName.
      Parameters:
      autoChildName - name of an AutoChild to customize the defaults for.
      defaults - Canvas defaults to apply. These defaults override any existing properties without destroying or wiping out non-overridden properties. For usage tips on this param, see SGWTProperties.
      See Also:
    • changeAutoChildDefaults

      public static void changeAutoChildDefaults(String autoChildName, FormItem defaults)
      Changes the defaults for FormItem AutoChildren named autoChildName.
      Parameters:
      autoChildName - name of an AutoChild to customize the defaults for.
      defaults - FormItem defaults to apply. These defaults override any existing properties without destroying or wiping out non-overridden properties. For usage tips on this param, see SGWTProperties.
      See Also:
    • changePickerIconDefaults

      public static void changePickerIconDefaults(FormItemIcon defaults)
    • setAccept

      public UploadItem setAccept(String accept)
      A comma-separated list of valid MIME types, used as a filter for the file picker window.

      Note that this property makes use of the HTML accept attribute, and so relies on the browser to perform the desired filtering. For further study, see:

      Parameters:
      accept - New accept value. Default value is null
      Returns:
      UploadItem instance, for chaining setter calls
    • getAccept

      public String getAccept()
      A comma-separated list of valid MIME types, used as a filter for the file picker window.

      Note that this property makes use of the HTML accept attribute, and so relies on the browser to perform the desired filtering. For further study, see:

      Returns:
      Current accept value. Default value is null
    • setCapture

      public UploadItem setCapture(String capture)
      This attribute enables camera capture functionality for mobile devices, accepting the following values:
      • Set it to "user" to capture using the front-facing camera.
      • Set it to "environment" to capture using the rear-facing camera.

      Please note that in the latest versions of Android and iOS, utilizing this attribute will consistently load the rear camera. This behavior is due to the direct camera software's ability to switch between the two cameras seamlessly.

      When working with the capture functionality of iPhones and Android devices, it's important to consider the supported DataSourceField.mimeTypes for audio, video, and image files that can be used with the fileItem.accept attribute. Here's a list of commonly supported mime types for capturing on these devices:

      Supported Image Mime Types:

      • image/jpeg: JPEG image format (.jpg, .jpeg)
      • image/png: Portable Network Graphics format (.png)
      Supported Audio Mime Types:
      • audio/3gpp: 3GPP format, commonly used for audio capture.
      • audio/mp4: MP4 format, widely supported for audio capture.
      Supported Video Mime Types:
      • video/3gpp: 3GPP format, commonly used for video capture.
      • video/mp4: MP4 format, widely supported for video capture.
      Behavior Based on 'accept' Attribute

      The behavior of using the capture attribute depends on the value used in the accept attribute. For example:

      • accept="image/*" will load the camera ready to take pictures.
      • accept="audio/*" will load the default audio recorder, not the camera.
      • accept="video/*" will load the camera in video mode, ready to capture videos.
      Please note that the Smart GWT framework does not control the native device behavior beyond these attribute settings.

      Lastly, keep in mind that these settings have no effect on desktop browsers; they apply exclusively to mobile devices.

      This information is "circa 2023" and may not apply to all devices.

      Parameters:
      capture - New capture value. Default value is null
      Returns:
      UploadItem instance, for chaining setter calls
    • getCapture

      public String getCapture()
      This attribute enables camera capture functionality for mobile devices, accepting the following values:
      • Set it to "user" to capture using the front-facing camera.
      • Set it to "environment" to capture using the rear-facing camera.

      Please note that in the latest versions of Android and iOS, utilizing this attribute will consistently load the rear camera. This behavior is due to the direct camera software's ability to switch between the two cameras seamlessly.

      When working with the capture functionality of iPhones and Android devices, it's important to consider the supported DataSourceField.mimeTypes for audio, video, and image files that can be used with the fileItem.accept attribute. Here's a list of commonly supported mime types for capturing on these devices:

      Supported Image Mime Types:

      • image/jpeg: JPEG image format (.jpg, .jpeg)
      • image/png: Portable Network Graphics format (.png)
      Supported Audio Mime Types:
      • audio/3gpp: 3GPP format, commonly used for audio capture.
      • audio/mp4: MP4 format, widely supported for audio capture.
      Supported Video Mime Types:
      • video/3gpp: 3GPP format, commonly used for video capture.
      • video/mp4: MP4 format, widely supported for video capture.
      Behavior Based on 'accept' Attribute

      The behavior of using the capture attribute depends on the value used in the accept attribute. For example:

      • accept="image/*" will load the camera ready to take pictures.
      • accept="audio/*" will load the default audio recorder, not the camera.
      • accept="video/*" will load the camera in video mode, ready to capture videos.
      Please note that the Smart GWT framework does not control the native device behavior beyond these attribute settings.

      Lastly, keep in mind that these settings have no effect on desktop browsers; they apply exclusively to mobile devices.

      This information is "circa 2023" and may not apply to all devices.

      Returns:
      Current capture value. Default value is null
    • setEditProxyConstructor

      public UploadItem setEditProxyConstructor(String editProxyConstructor)
      Default class used to construct the EditProxy for this component when the component is first placed into edit mode.
      Overrides:
      setEditProxyConstructor in class TextItem
      Parameters:
      editProxyConstructor - New editProxyConstructor value. Default value is "FileItemEditProxy"
      Returns:
      UploadItem instance, for chaining setter calls
      See Also:
    • getEditProxyConstructor

      public String getEditProxyConstructor()
      Default class used to construct the EditProxy for this component when the component is first placed into edit mode.
      Overrides:
      getEditProxyConstructor in class TextItem
      Returns:
      Current editProxyConstructor value. Default value is "FileItemEditProxy"
      See Also:
    • setHeight

      public UploadItem setHeight(int height)
      Height for this uploadItem. Note that Smart GWT will not apply this size to the native HTML <input ...> element written out by this formItem as this leads to inconsistent appearance across different browsers. The specified height acts as a minimum cell width for the item.
      Overrides:
      setHeight in class FormItem
      Parameters:
      height - New height value. Default value is 19
      Returns:
      UploadItem instance, for chaining setter calls
      See Also:
    • getHeight

      public int getHeight()
      Height for this uploadItem. Note that Smart GWT will not apply this size to the native HTML <input ...> element written out by this formItem as this leads to inconsistent appearance across different browsers. The specified height acts as a minimum cell width for the item.
      Overrides:
      getHeight in class FormItem
      Returns:
      Current height value. Default value is 19
      See Also:
    • setMultiple

      public UploadItem setMultiple(Boolean multiple)
      When true, allow the file-selection dialog shelled by the browser to select multiple files.

      Support is not full-cycle at the server - that is, there are server APIs for retrieving each file that was uploaded, but no built-in support for storing multiple files against a single DataSource field. However, you can write custom server DMI code to do something with the files - for instance, you could create multiple new DataSource records for each file via a server DMI like this below:

           String fileNameStr = (String)dsRequest.getValues().get("image_filename").toString();
       
           String[] fileNames = fileNameStr.split(", ");
           List files = dsRequest.getUploadedFiles();
       
           for (int i = 0; i < files.size(); i++) {
               ISCFileItem file = (ISCFileItem)files.get(i);
               InputStream fileData = file.getInputStream();
               DSRequest inner = new DSRequest("mediaLibrary", "add");
               Map values = new HashMap();
               values.put("title", dsRequest.getValues().get("title"));
               values.put("image", fileData);
               values.put("image_filename", fileNames[i]);
               values.put("image_filesize", file.getSize());
               values.put("image_date_created", new Date());
               
               inner.setValues(values);
               inner.execute();
           }
           
           DSResponse dsResponse = new DSResponse();
           
           dsResponse.setStatus(0);
       
           return dsResponse;
        
      Overrides:
      setMultiple in class FormItem
      Parameters:
      multiple - New multiple value. Default value is true
      Returns:
      UploadItem instance, for chaining setter calls
      See Also:
    • getMultiple

      public Boolean getMultiple()
      When true, allow the file-selection dialog shelled by the browser to select multiple files.

      Support is not full-cycle at the server - that is, there are server APIs for retrieving each file that was uploaded, but no built-in support for storing multiple files against a single DataSource field. However, you can write custom server DMI code to do something with the files - for instance, you could create multiple new DataSource records for each file via a server DMI like this below:

           String fileNameStr = (String)dsRequest.getValues().get("image_filename").toString();
       
           String[] fileNames = fileNameStr.split(", ");
           List files = dsRequest.getUploadedFiles();
       
           for (int i = 0; i < files.size(); i++) {
               ISCFileItem file = (ISCFileItem)files.get(i);
               InputStream fileData = file.getInputStream();
               DSRequest inner = new DSRequest("mediaLibrary", "add");
               Map values = new HashMap();
               values.put("title", dsRequest.getValues().get("title"));
               values.put("image", fileData);
               values.put("image_filename", fileNames[i]);
               values.put("image_filesize", file.getSize());
               values.put("image_date_created", new Date());
               
               inner.setValues(values);
               inner.execute();
           }
           
           DSResponse dsResponse = new DSResponse();
           
           dsResponse.setStatus(0);
       
           return dsResponse;
        
      Overrides:
      getMultiple in class FormItem
      Returns:
      Current multiple value. Default value is true
      See Also:
    • setTextBoxStyle

      public UploadItem setTextBoxStyle(String textBoxStyle)
      Base CSS class name for this UploadItem's native file input element.

      Note that the customization via CSS of a native file input element allowable by the browser varies widely; in some browsers on certain platforms, it may be possible to customize certain CSS properties, but not in others; or, it may be that the CSS property (e.g. border) is applied differently in some browsers.

      If the textBoxStyle is changed at runtime, FormItem.updateState() must be called to update the visual state. However, calling updateState() will clear any file selected by the user to be uploaded.

      Overrides:
      setTextBoxStyle in class TextItem
      Parameters:
      textBoxStyle - New textBoxStyle value. Default value is "uploadItem"
      Returns:
      UploadItem instance, for chaining setter calls
      See Also:
    • getTextBoxStyle

      public String getTextBoxStyle()
      Base CSS class name for this UploadItem's native file input element.

      Note that the customization via CSS of a native file input element allowable by the browser varies widely; in some browsers on certain platforms, it may be possible to customize certain CSS properties, but not in others; or, it may be that the CSS property (e.g. border) is applied differently in some browsers.

      If the textBoxStyle is changed at runtime, FormItem.updateState() must be called to update the visual state. However, calling updateState() will clear any file selected by the user to be uploaded.

      Overrides:
      getTextBoxStyle in class TextItem
      Returns:
      Current textBoxStyle value. Default value is "uploadItem"
      See Also:
    • setWidth

      public UploadItem setWidth(int width)
      Width for this uploadItem. Note that Smart GWT will not apply this size to the native HTML <input ...> element written out by this formItem as this leads to inconsistent appearance across different browsers. The specified width acts as a minimum cell width for the item.
      Overrides:
      setWidth in class FormItem
      Parameters:
      width - New width value. Default value is 150
      Returns:
      UploadItem instance, for chaining setter calls
      See Also:
    • getWidth

      public int getWidth()
      Width for this uploadItem. Note that Smart GWT will not apply this size to the native HTML <input ...> element written out by this formItem as this leads to inconsistent appearance across different browsers. The specified width acts as a minimum cell width for the item.
      Overrides:
      getWidth in class FormItem
      Returns:
      Current width value. Default value is 150
      See Also:
    • deselectValue

      public void deselectValue()
      This method is not supported by UploadItem.
      Overrides:
      deselectValue in class TextItem
    • deselectValue

      public void deselectValue(Boolean start)
      This method is not supported by UploadItem.
      Overrides:
      deselectValue in class TextItem
      Parameters:
      start - By default the text insertion cursor will be moved to the end of the current value - pass in this parameter to move to the start instead
    • getSelectionRange

      public int[] getSelectionRange()
      This method is not supported by UploadItem.
      Overrides:
      getSelectionRange in class TextItem
      Returns:
      null
    • selectValue

      public void selectValue()
      This method is not supported by UploadItem.
      Overrides:
      selectValue in class TextItem
    • setSelectionRange

      public void setSelectionRange()
      This method is not supported by UploadItem.
    • setValue

      public void setValue()
      Attempting to set the value for an upload form item is disallowed for security reasons. Therefore this method will just log a warning, and not modify the value of the item.
    • setDefaultProperties

      public static void setDefaultProperties(UploadItem uploadItemProperties)
      Class level method to set the default properties of this class. If set, then all existing and subsequently created instances of this class will automatically have default properties corresponding to the properties of the class instance passed to this function. This is a powerful feature that eliminates the need for users to create a separate hierarchy of subclasses that only alter the default properties of this class. Can also be used for skinning / styling purposes.

      Note: This method is intended for setting default attributes only and will affect all instances of the underlying class (including those automatically generated in JavaScript). This method should not be used to apply standard EventHandlers or override methods for a class - use a custom subclass instead. Calling this method after instances have been created can result in undefined behavior, since it bypasses any setters and a class instance may have already examined a particular property and not be expecting any changes through this route.

      Parameters:
      uploadItemProperties - properties that should be used as new defaults when instances of this class are created
      See Also: