Custom form - multiple file upload


(Gavin) #1

Matrix Version:
5.5.3.3

Hi,

If I have a file question on my custom form and I set the “max number of files” setting to more than 1, how do i use keywords to reference the additional files ?

For example:

%response_320400_q4_file_size%
%response_320400_q4_file_content%

Thanks.


(Jsisk) #2

Hi Gavin,

Default output that I can see for
%response_320400_q4%

will be something like
image.jpg, type image/jpeg, 1.1 MB | image2.jpg, type image/jpeg, 1.1 MB | image3.jpg, type image/jpeg, 303.7 KB

A pipe separated list of the file name, the file type and its size.
I don’t think there’s any native keyword for each file uploaded unfortunately.

Is there something else you’d need other than that?

Can you explain why?


(Gavin) #3

When the files are submitted, I’m base64 encoding them and sending them to a rest service, so I need the content and the size for each file for which the keywords above are essential.


#4

Hi Gavin,

I have precisely this issue with a form that I’m working on right now.

What I’ve been told is that this keyword should work:
%response_320400_q4_file_content%

And would return an Array of file contents. However, there is a bug that needs to be fixed which is causing that keyword to create a PHP error.

So, the workaround that I’ve got is the below.

You need to split the uploaded file IDs into individual IDs and then use the ^as_asset keyword modifier to get the data you need.

That is, this keyword gives you the file name of the first file uploaded:
%response_<parent_id>_<question_id>_file_id^explode:, ^index:0^as_asset:asset_attribute_name%'

And this will give you the contents of the first file uploaded:
%response_<parent_id>_<question_id>_file_id^explode:, ^index:0^as_asset:asset_file_contents^base64encode%

You just increase that index value for each file you have. So ^index:1 gives you the second file, and so on.

The big catch here is that the files must have a Live Status and be publicly accessible in order for this workaround to work.

Since you are using a form file field you will need to create a new Matrix Trigger to watch for files created in your file upload folder and then set their Status to be Live.


(Gavin) #5

Thank you for your comprehensive reply James, much appreciated.
I did dabble with this approach for a single file but didn’t realise that multiple files would be saved as an array.
Unfortunately the likelihood is that I need to have create file off (as the documents need to be sent on rather than stored for security reasons) which means(I think) there is no file id.
Its good to know its a recognised bug though, hopefully there will be some progress on it soon.