Asset listing - show multiple form uploads


#1

Matrix Version: v5.5.6.4

We’re using an asset listing to display custom form uploads as links.

Using a hardcoded URL and keywords works for the first upload.

<a href="https://www.site.com/uploads/%question_answer_<parent_id>_<question_id>^maxwords:1^replace:,:%">%question_answer_<parent_id>_<question_id>^maxwords:1^replace:,:% 
</a>

However as maxwords:1 is needed for the hardcoded URL to work it will only display the first upload and nothing else if there are multiple uploads. Have tried using ^as_asset:asset_url modifier instead of a hardcoded URL but that comes up blank.

Any ideas?


(Bart Banda) #2

I think this would be easiest solved with SSJS and using the %asset_data_attributes% keyword on each submission to get the list of file submissions. Do you have experience with that to be able to achieve it that way?


(Mark Nearhos) #3

Have you tried something like this:
<a href="./?a=%question_answer_453360_q19_file_id%">%question_answer_453360_q19_file_id^as_asset:asset_name%</a> (%question_answer_453360_q19_file_id^as_asset:asset_file_size_readable% )


(John) #4

Hi, I am also trying to do this. Did you manage to get it working?

I am trying to pull out the list of uploaded files (from a multi file upload field) using SSJS and %asset_data_attributes%. I need to make these into clickable links. Other suggested methods above only return the first uploaded file.


(Chris Mackay) #5

How is the data coming through as SSJS?

When I have set up a form with the multiple upload option enabled, the payload being returned is 1 single string.

Looks something like this:

“attachments”: “Uploaded1.pdf, type application/pdf, 10.2 KB | Uploaded2.pdf, type application/pdf, 10.2 KB | anyname.pdf, type application/pdf, 10.2 KB”

This string then needs to be split into its own array (using PDF as the only file type for this example):

const data = [{“attachments”: “Uploaded3.pdf, type application/pdf, 10.2 KB | Uploaded4.pdf, type application/pdf, 10.2 KB | anyname.pdf, type application/pdf, 10.2 KB”}];

const assets = data.filter(item => item.attachments.length).map(item => {

const attachments = item.attachments.split(" | ");

return attachments.filter(attachment => attachment.endsWith(".pdf"));

}).flat();

print(assets);

Once you have this, you should be able to iterate through using a forEach, and ammend the URL to each of the assets.

Hope this helps. I am sure there may be other Squiz specific ways to do this, but this is what worked for me.


(John) #6

Thanks for the advice! While doing this I noticed that I could get a comma separated list of the attached file id’s using %question_answer_12345_q1_file_id% so I ended up using something like:

<script runat="server">
var attachment = '%question_answer_12345_q1_file_id^replace:[ ]:%';
var attachmentArr = attachment.split(',');
attachmentArr.forEach((element) => {
        print('<a href="./?a=' + element + '" download="">' + '%globals_asset_name:' + element + '%' + '</a> ');
    }
);
</script>