Using the File Copy Server Action

We start with a very simple data table containing an primary key (imageid) and and image name (imagename)

image

Each image is stored in a folder called “images1”

We wish to move these images into a new folder with each image in a unique folder based on their id triggered by a button click

for example, /images/1/images/2 etc

Firstly we create a simple bootstrap table using generator

App connect and bootstrap have already been added to the page as has a container/row/column

image

we create a server Action to retrieve the image data

Create a server connection

Then a query to retrieve all the image data

Don’t forget to save it, i called this getimages

Now in app connect make a Server Connection to that server action, i called this conn_getimages

image

Now use generator to create a bootstrap table based on that server action

The output should be:

Now let us change the text of the image name to an actual image

Click on the cell containing the image name and insert an image inside it

Now go to the Dynamic Attributes of the image and select image source

Select the data picker, switch to template and add the path then the imagename.
Be sure to select the imagename from the repeat region, NOT the server connection

Click Select

Lastly click the containing cell and uncheck the inner text entry

You may need to limit the image size, to do so select dynamic Properties of the image and set the max height or width. I use 200pixels width

image

You will now see the images on screen

Now we need a move button. We will send parameter via POST to allow additional parameters to be sent if required.but an anchor button or link and GET parameter would suffice in simple cases

Select the first table cell and insert a form inside, i will call form_img and set it’s method to post


image

Inside the form add a text input

and a button

Give the text input a name, i use imageid to match the table column we will be querying on and make that input hidden

image

Lastly colour the button to your taste and make it a submit button

image

Again don’t forget to remove the parent cell’s inner text setting by unticking the check box

image

Lastly click the button and change it’s title to “Move”

Save ther page!

Now we need the server action which will move the file

Ctreatye a new server action, i will call it filemover

In globals browse to your page and select the form you have just created. Click Import from Form to get the parameters from the form, in this case imageid

We will now copy the file from the folder images1 to to a second folder /images2/ and within that a sub folder corresponding to the imageid i.e. /images2/{{imageid}}

Firstly we need the image name

Add a database connection

image

Then a database query to retrieve the image name from the imageid passed from the form

image
image

filtered on the imageid sent from the form

Save your server action

Add a new step, Core Actions => Repeat

and set it’s expression to the results of the query and output the imagename field

image

Now we add the file move within the repeat

In the server action add a new step, File Management -> File Copy

In the File Path set the original file’s folder and the imagename returned by the server action as shown in the repeat

Now set the destination path, in this case it will be images2 followed by the imageid. It will be given the same filename

Ensure you check “Create Folder”

image

Click OK and Save the server Action

We now go back to the app connect page and select the form we called form_img and set it to a server connect form and link it to the new server action.

image

lastly let us set a dynamic event for the form based on the success event.

Select Dynamic Events for the form => Server Connect => Success

Click the picker

image

and select the server connection load event

Now we will add a second table to show the results.

Select the first container and click duplicate (it’s just a quick way of adding a second almost identical table)

image

Now scroll down to the image component in this duplicated container and change the path to:

/images2/{{imageid}}/{{imagename}}

image

Now remove the buttons from the second container form (as we would now want to move the moved file!)

The new table will correctly show a broken image icon as these images do not yet exist

Open the page in your preferred browser

Click a move button, pause for a second or two for the image to be moved on the server and then refresh

The image has been copied to the new location and will show in lower table

2 Likes

Great, great job
Tonight I try and let you know
Thank you very much

Hello Hyperbytes
I studied your tutorial and I seem to have understood to the point where the MoveImage Insert action


My problem at this point is that for each record I have several fields with image names (whose files are all in the same folder server).
At this point maybe I should work like this:
1- create a query for each file field
2- check if it is empty or not
3- copy the file to the new folder
4- save name and code image to the new database folder

And repeat the operation for each image.
Is my reasoning right or not?
Thanks for your assistance

yes, if there are multiple images, create a query, use that as the expression for a repeat and insert the copy or move inside the repeat.

you then move from:
/sourcedirectory/{{imagename}}
to
/destination

ok but I did not understand why should I use Repeat if I have to copy only one image at a time?
In practice, when I create the file_copy action,
I import the page record list containing the submit button form
Then I create a connection to the database and a query in which I insert the name of the name file I want to copy (conditional on the id of the variable Post)
Then I call the filecopy action, but here I stop because I don’t know how to proceed.
In fact I can’t find the file name

Perhaps it is important that I better explain my need
In the old table, each record has several fields with the names of the xx images present in a single server folder (folder origin)
Having created a gallery fully functional with Wappòer I should copy or transfer the images to another foder (folder destination) by creating sub-folders (with names equal to ID) and if possible also creating thumbnails in another sub-folder
Furthermore I should be saved the name of the file and its reference ID in the images folder of the gallery
It is for these reasons that I have lost a little

Hi Marzio

The need fro a thumbnail changes the process completely! I wish you had mentioned that earlier.

I have NOT tested this but the process should be similar to:

Query your existing table like this: (changing field names to match yours of course)

Set up a repeat with that query1 as the expression outputting fields you need:

Add Image processor => Load Image loading the original file from /youroldpath/{{imagename}}

Now save the image to the new path, Image Processor => Save Image

Resize the image with Image processor=>Resize Image, i have picked 150 x 150 pixels as you don’t specify the size you want.

and save the thumbnail as before with Image Processor => Save Image , you don’t say where you want it saved so i have picked a sub directory /thumb/

I am not sure why you need to re-store the image path as it can be calculated from the {{imageid}} and {{imagename}} (perhaps it’'s a different table) however you would then do a table insert

The repeat will loop through all your images (may take a while dependant on number of images)

Almost everything is clear to me.
But I still don’t understand why the repeat
In fact the action acts on only one record at a time and the names of 30 image files are in 30 different fields, so I think it is appropriate to structure the action by copying one file at a time
Tell me if this can be the correct procedure:

  • Query to identify the image file 01
  • Check with if Exist if the filename exists or is null
  • If there exists load image 01
  • Processor image 01
  • Save image 01
  • Same procedure for thumbnail
  • Add name in gallery table
    Following the same procedure for the other 29 images

This is because I find it difficult to understand if I can automate using the repeat
If it is correct the only point I do not understand is how to load the files by taking the names from the queries

Then repeat is for two reasons.

Firstly the obvious, you need to repeat through all images if more than one returned by the query, secondly you need a repeat on a query to access it’s contents.

Even if there is only 1 result you must still use a repeat, it is how Wappler makes the query contents available to later stages. Without it step two in your list couldn’t access the data from the query

You need:

  • Query to identify the image file 01
  • Repeat on query results (even if only 1 result returned)
  • Check with if Exist if the filename exists or is null
  • If there exists load image 01
  • Processor image 01
  • Save image 01
  • Same procedure for thumbnail
  • Add name in gallery table

Follow the same procedure for the other 29 images

1 Like

Now I get it. It was that repeat that took me off-road
I was looking for an alternative to read the image without using the repeat
Thank you also for your patience

Hi Hyperbytes
I have set up the whole procedure for all 30 images of each record.
I had to change it a bit to make it work:
I have yet to insert a progress bar, but everything works perfectly
Thank you

Great, delighted it worked