Skip to main content
Version: 8.1

Uploading a File

The PersistentRecord class can be used to build in the option for your project to be able to upload a file to the Gateway. Although, there are a few ways this can be accomplished, and some light testing will be needed to customize this for each project, the general approach described below will help you get started.

In this example, you'll begin by defining a KeystorePath of StringField type to your PersistentRecord definition to populate upload forms.

public static final StringField KeystorePath = new StringField(META, "keystorePath", SFieldFlags.SMANDATORY)
.setDefault("");

Then, add the associated get function to make sure you can retrieve form inputs.

static {
KeystorePath.getFormMeta().setEditorSource(FileUploadEditorSource.getSharedInstance());
}

Now, you'll want to enable the upload functionality, set up a way to hold the uploaded file, and capture the value in the field. This can be done by subclassing RecordEditForm, using a private final field for FileUploadField, and overriding the newEditorComponent function to capture, in this example, the value of KeystorePath.

private FileUploadField uploadField;

...

@Override
protected Component newEditorComponent(String id, FormMeta formMeta, RecordEditMode mode, SRecordInstance record) {
Component comp = super.newEditorComponent(id, formMeta, mode, record);
if (formMeta.getField().equals(KeystorePath.getForm().getField())) {
uploadField = (FileUploadField) ((AbstractFormComponentEditor<?>) comp).getFormComponent();
}

Finally, you can override the onSubmit function to extract the list of uploaded files.

List<FileUpload> fuList = uploadField.getModelObject();