Class AjaxUploadManager


  • public final class AjaxUploadManager
    extends Object
    Manages the files uploaded via AJAX before the form was submitted. Files uploaded via AJAX are stored in a temporary directory. The name of this directory is stored in the form request session and deleted when the form request session is closed.

    The form request session contains a mapping between the name of the name of the upload element (e.g. upl1_dyn_fs_1) and the descriptor of the file that was uploaded. The descriptor stores the path to the temporary upload file, as well as some meta data such as the file name and content type and a unique UUID for the file.

    The process to upload a file via AJAX is as follows:

    1. The user selects a file in the browser via a native file input element
    2. The client side JavaScript generates a unique AJAX UUID and stores it in a hidden input field next to the file input element
    3. The client side JavaScript sends the name of the file input element, the AJAX UUID, the file's contents and its meta data to the server
    4. The server creates a new entry in the upload map in the form request session
    5. The user later submits the form to the server. This sends the UUID in the hidden input field
    6. For each upload element for which an AJAX UUID was submitted, the server retrieves the entry in the upload map. If a matching entry is found, it merges the uploaded file into the request data.
    The advantage of using an AJAX UUID is that the form is in control of whether a file should be submitted and which file should be submitted. Clearing the hidden input element ensures that no upload is used, regardless of the server state.
    Since:
    7.0.11
    Author:
    XIMA MEDIA GmbH
    • Method Detail

      • addAllToParametersWrapper

        public static void addAllToParametersWrapper​(FRQSession session,
                                                     ParameterWrapper parameterWrapper)
        Adds all AJAX uploads from the session to the given parameters wrapper.
        Parameters:
        session - Current form request session.
        parameterWrapper - Parameters wrapper to which to add uploads.
      • addUpload

        public static void addUpload​(FRQSession session,
                                     long time,
                                     String id,
                                     String fieldName,
                                     String md5Sum,
                                     String contentType,
                                     String fileName,
                                     InputStream data)
                              throws AjaxUploadException
        Registers an uploaded file with the given session.
        Parameters:
        session - Form request session with the state.
        time - Time when the request was made. Note that this is not required to correspond to a physicial time, the only requirement for this number is that a larger number implies that a request was made later.
        id - Unique ID of the upload.
        fieldName - Field name of the upload.
        md5Sum - Expected MD5 sum of the entire file. This is also used as the ID of the file.
        contentType - Content type of the file.
        fileName - Original file name of the file.
        data - Data of the file.
        Throws:
        AjaxUploadException - When the upload could not be added, such as when the temporary directory could not be created.
      • getUploadFileMeta

        public static Map<String,​UploadFileMeta> getUploadFileMeta​(FRQSession session)
        Finds all existing uploads that were registered in the given session (and not yet removed).
        Parameters:
        session - Form request session with the state.
        Returns:
        A map with all existing uploads. Key is the file ID.
      • removeUpload

        public static void removeUpload​(FRQSession session,
                                        long time,
                                        String fieldName)
        Removes an uploads that was added via previously.
        Parameters:
        session - Form request session with the state.
        time - Time when the request was made. Note that this is not required to correspond to a physicial time, the only requirement for this number is that a larger number implies that a request was made later.
        fieldName - Unique name or ID of the upload.
      • fetchUploadData

        public static FetchUploadDataResult fetchUploadData​(FRQSession session,
                                                            String id,
                                                            String uploadName)
                                                     throws IOException
        Fetches the data registered with the given upload name, when it matches the ID.
        Parameters:
        session - Form request session with the state.
        id - The unique ID of the file.
        uploadName - Unique name or ID of the upload.
        Returns:
        The data of the given upload.
        Throws:
        IOException - When the data could not be fetched or does not exist.