Class FileElement


  • public class FileElement
    extends InputElement
    Represents an InputElement which is a file select control. A file select control allows the user to select one or more files to upload.

    e.g. <input name="uploadedfile" type="file" />

    If files are added to a FileElement object, they will be uploaded when the containing Form is sent to the web server.

    See Also:
    Form
    • Constructor Detail

      • FileElement

        public FileElement​(long cPtr,
                           boolean cMemoryOwn)
        For internal use only. Of no interest to the user.
        Parameters:
        cPtr - pointer to the CPP wrapped object
        cMemoryOwn - indicates if this object is responsible for memory management of the CPP object
      • FileElement

        public FileElement​(java.lang.String elementName)
        Creates a FileElement object with the specified name
        Parameters:
        elementName - the name attribute of the FileElement
    • Method Detail

      • getCPtr

        public static long getCPtr​(FileElement obj)
        For internal use only. Of no interest to the user.
        Parameters:
        obj - a reference to an object of this class
        Returns:
        a long containing the address of the CPP wrapped object
      • addFile

        public void addFile​(java.lang.String filename,
                            java.lang.String path,
                            java.lang.String contentType,
                            java.lang.String contentTransferEncoding)
                     throws java.io.UnsupportedEncodingException
        Adds a file to the list of files to upload. It will be uploaded when the Form which contains this FileElement is sent to the web server.
        Parameters:
        filename - the name of the file
        path - the location of the file on disk. This can either be an absolute path, or a relative path. If it is a relative path, then it is relative to the data/Files directory for the current injector
        contentType - the Content-Type to specify when sending this file (e.g. "text/plain", "image/gif")
        contentTransferEncoding - the Content-Transfer-Encoding to specify when sending this file (e.g. "binary")
        Throws:
        java.io.UnsupportedEncodingException - The specified encoding is not supported.
      • addFile

        public void addFile​(java.lang.String filename,
                            java.lang.String path,
                            java.lang.String contentType)
                     throws java.io.UnsupportedEncodingException
        Adds a file to the list of files to upload. It will be uploaded when the Form which contains this FileElement is sent to the web server.

        The default Content-Transfer-Encoding is "".

        Parameters:
        filename - the name of the file
        path - the location of the file on disk. This can either be an absolute path, or a relative path. If it is a relative path, then it is relative to the data/Files directory for the current injector
        contentType - the Content-Type to specify when sending this file (e.g. "text/plain", "image/gif")
        Throws:
        java.io.UnsupportedEncodingException - The specified encoding is not supported.
      • addFile

        public void addFile​(java.lang.String filename,
                            java.lang.String path)
                     throws java.io.UnsupportedEncodingException
        Adds a file to the list of files to upload. It will be uploaded when the Form which contains this FileElement is sent to the web server.

        The default Content-Type is "text/plain", and the default Content-Transfer-Encoding is "".

        Parameters:
        filename - the name of the file
        path - the location of the file on disk. This can either be an absolute path, or a relative path. If it is a relative path, then it is relative to the data/Files directory for the current injector
        Throws:
        java.io.UnsupportedEncodingException - The specified encoding is not supported.
      • removeFile

        public void removeFile​(java.lang.String filename)
                        throws java.io.UnsupportedEncodingException
        Removes a file from the list of files to be uploaded.
        Parameters:
        filename - the name of the file to remove from the list
        Throws:
        java.io.UnsupportedEncodingException - The specified encoding is not supported.
      • clearFiles

        public void clearFiles()
        Clears the list of files to be uploaded.
      • getMultipartBoundary

        public java.lang.String getMultipartBoundary()
        Gets the String that will be used as the boundary between files (including the -- prefix), when encoding this element as multipart/form-data.

        e.g. for this POST data message body:

        
         --multipart-form-boundary
         Content-Disposition: form-data; name="input1"
        
         value1
         --multipart-form-boundary
         Content-Disposition: form-data; name="files-to-upload[]"
         Content-Type: multipart/mixed; boundary=multipart-file-boundary
         
         --multipart-file-boundary
         Content-Disposition: file; filename="image.bmp"
         Content-Type: image/bmp
         
         (binary image data)
         --multipart-file-boundary
         Content-Disposition: file; filename="image2.bmp"
         Content-Type: image/bmp
        
         (binary image data)
         --multipart-file-boundary--
         --multipart-form-boundary--
         
        form.getFileElement("files-to-upload[]").getMultipartBoundary() will return "--multipart-file-boundary"
        Returns:
        the String that will be used as the boundary between files (including the -- prefix), when encoding this element as multipart/form-data
        See Also:
        setMultipartBoundary(String)
      • setMultipartBoundary

        public void setMultipartBoundary​(java.lang.String boundary)
        Sets the String that will be used as the boundary between files (including the -- prefix), when encoding this element as multipart/form-data.

        e.g.

        
         // Populate form with data
         Form form = previousResponse.extractForm("myForm");
         form.getInputElement("input1").setValue("value1");
         form.getFileElement("files-to-upload[]").addFile("image.bmp", "C:\\dataFiles\\image.bmp", "image/bmp");
         form.getFileElement("files-to-upload[]").addFile("image2.bmp", "C:\\dataFiles\\image2.bmp", "image/bmp");
        
         // Set the multipart boundary for the FileElement
         form.getFileElement("files-to-upload[]").setMultipartBoundary("--multipart-file-boundary");
        
         // Send the POST request
         Request request = getWebBrowser().createRequest(HttpMethod.POST, myUrl);
         request.setMessageBody(form);
         Response response = request.send();
         
        The POST data message body contains:
        
         --multipart-form-boundary
         Content-Disposition: form-data; name="input1"
        
         value1
         --multipart-form-boundary
         Content-Disposition: form-data; name="files-to-upload[]"
         Content-Type: multipart/mixed; boundary=multipart-file-boundary
         
         --multipart-file-boundary
         Content-Disposition: file; filename="image.bmp"
         Content-Type: image/bmp
         
         (binary image data)
         --multipart-file-boundary
         Content-Disposition: file; filename="image2.bmp"
         Content-Type: image/bmp
        
         (binary image data)
         --multipart-file-boundary--
         --multipart-form-boundary--
         
        Parameters:
        boundary - the String that will be used as the boundary between files (including the -- prefix), when encoding this element as multipart/form-data
        See Also:
        getMultipartBoundary()
      • clone

        public HtmlElement clone()
        Description copied from class: HtmlElement
        Creates a copy of this HTML element.
        Overrides:
        clone in class InputElement
        Returns:
        a new HtmlElement object which is an exact copy of this HTML element