Smart Action file form field

Hello,

I did not find any documentation about using file field in smart action.

I would like to use a file field with restrictions on file type (only gif, one, jpeg) base on file extension or mime type.

Then, when retrieving value, I need Base64 or binary representation, selected filename and mime type.

Here is my form field declaration :

{
label: ‘selectedFile’,
type: ‘File’,
isRequired: true,
}

And retrieving code :
const pdfFile = context.formValues[‘selectedFile’];
const pdfContent = pdfFile.buffer;

But didn’t know what type of pdfContent is.

Any help would be appreciated (or documentation pointer).

Regards,

David

Hello @dscreve,
You seem to be using agent node-js package.
If this is indeed the case, you will find the doc for the file picker widget here, along with a in-depth example:

Please let me know if this would help you out.
Regards,

@Nicolas.M

hum…it seems the doc is not correct :

    const pdfFile: File = context.formValues['PDF File'];
        console.log('pdfFile : '+JSON.stringify(pdfFile));
        fileName = pdfFile.name;
        typeMime = pdfFile.type;
        fileContent  = await pdfFile.arrayBuffer();

Actually, context.formValues[‘PDF File’] is not a File type as shown in the doc (buffer is not a member of File)…but I don’t know the type of it…

Regards,

David

Right, I think I see where you might be confused:

context.formValues['PDF Files'] is indeed not a native javascript/typescript File type object, but as shown at the top of the example its type is imported from @forestadmin/datasource-toolkit

import { File } from '@forestadmin/datasource-toolkit';

therefore it doesn’t expose arrayBuffer() method directly, but a buffer property

its type definition is the following:

export type File = {
  mimeType: string;
  buffer: Buffer;
  name: string;
  charset?: string;
};

Can you use this ?
If you still encounter issues, could you please provide more details about your use case, following the template:

Feature(s) impacted

Observed behavior

Expected behavior

Failure Logs

Context

  • Project name: …
  • Team name: …
  • Environment name: …
  • Agent (forest package) name & version: …
  • Database type: …
  • Recent changes made on your end if any: …

Thanks

This works fine !

Thanks for your help.

1 Like