How to perform a loop on a file?

Hello everyone…

I’m uploading some files into the File Manager and inserting those files into the Database.
Along with the files will be another field called Order to specify the order of each file.

And to do that I have to perform a loop. (as far as I know)

I have set a workflow that works somewhat.
The only issue is that it loops for every single file. (I have uploaded 3 images and it run 9 times. 3 for each file)

--bw-00

The backend workflow


--bw-02
--bw-03

The workflow when the button is clicked

What I’m doing wrong?
I hope anyone can help me.

Thanks
Fabian

That’s because you’re running it on a list.

Don’t do that (that’s not a loop)… just include the list in a parameter on the workflow, and loop through each item until they are all complete.

if i don’t do it on a list it will not take the hole list but just the first file

That’s because you haven’t set the parameter to be a list.

and how I do that? which parameter to chose?

should it be a list?

Just tick the ‘Is a list/array’ box next to the relevant parameter.

1 Like

and now it doesn’t read which file it is
--bw-05

I tried with URL and Filename also…

Are you telling the workflow which ‘file’ to run on?

of course

--bw-06

its the same that I used on the Schedule API on a List

No… there your just defining a list of files to pass into the workflow…

You have to define which file from that list to work on during each iteration of the workflow. Are you doing that correctly (in your workflow actions)?

Also, I recommend you rename your current ‘file’ parameter to ‘files’ (as it’s a list)… otherwise it will get very confusing to understand what you’re doing.

What do you mean to define which file? I thought that it will take by itself.

I did a copy of the page to another “app” to show exactly what I did (you can’t run the API here because it is a free plan)
https://bubble.io/page?type=page&name=index&id=upload-demo&tab=tabs-1


I’m providing the screenshots also

The backend API:



The front

I’m using this plugin for uploading

https://bubble-essential-plugins.bubbleapps.io/version-test/file-multifile-up

I thought that it will take by itself.

No, the workflow will only do what you tell it to… it won’t take care of anything by itself (thankfully)…

So, looking at your app, here is what your workflow is currently doing:

  1. It’s taking a list of files as a parameter…

  2. Then it’s creating a single Thing (called uploaded files) and assigning a value to the Image field, that value being the entire list of files you passed into the workflow.

Which doesn’t make any sense, and won’t work… (an image field can only take a single image, not a list of images)

A list of files, is actually just a list of URLs anyway… so your image field is being set to something like: https://s3.amazonaws.com/appforest_uf/file_url,https://s3.amazonaws.com/appforest_uf/file_url,https://s3.amazonaws.com/appforest_uf/file_url,https://s3.amazonaws.com/appforest_uf/file_url
(i.e. a list of URLs separated by a comma)

Which, obviously, is not a valid URL, so your image won’t have a valid, displayable image.

And then you’re looping that workflow, once for each file in the list… so you’ll just end up with list of newly created things, none of which have an actual image.

I’m guessing, what you’re actually trying to do is create one new thing for each file in the list?

In which case, you have to define which file from the list you’re using to set the Image field on the new thing in each run of the workflow.

There are various ways do to that, but probably the easiest is just to use the ‘First’ item from the list each time, and then remove that item from the list for the next run of the workflow.

You’ll ned to change your condition on the reschedule action for that to work though.

Alternatively, you can add use the Order Number parameter on the workflow to keep track of which item you’re working on in any given run.

I’ve made some changes to your demo app, and added another version using the second method, so you can see how it should be done.

1 Like

No, the workflow will only do what you tell it to… it won’t take care of anything by itself (thankfully)…

All the tutorials that I have watched or read was using the user type as an example. They just put the list there and I thought of doing the same… (no one explained properly as see now)

I saw what you did in the backend workflow…

Thank you very much @adamhholmes. I couldn’t ever do that alone.

Files:minus item files:first item
files:count >1

No way a beginner could think of that :sweat_smile:
It looks like code to me…

Anyway…, thank you very much
Fabian

1 Like

This topic was automatically closed after 70 days. New replies are no longer allowed.