KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to save images in a folder (Client-Server or 4D Single User)
PRODUCT: 4D | VERSION: 11.4 | PLATFORM: Mac & Win
Published On: September 17, 2009
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

In some cases storing images in a folder outside of 4D is required. This solution will work on Client-Server as well as 4D Single User.

We need to create a folder to store the images. In this case we will put the folder Images next to the structure.

In your code you should test for the folder and if it does not exist, you should create it.

The idea is to create a path to the folder so we can copy the image file to it. In order to have it work on Client-Server or Single user, store the paths in interprocess variables.

To obtain the path on the Server we want to use the following method in the On Server Startup Database Method. Make sure the Get_ImagesPath_Server method is marked as Execute on Server on the Method Properties.


   `..............................................................
   ` Project Method: Get_ImagesPath_Server
   ` Description: gets the Server path to Images folder
   ` Parameters: None
   ` Created by luis pineiros
   ` 2009
   `..............................................................

C_TEXT($path)
C_TEXT(<>ImagePath_Server_t)

$path=Get 4D folder(Database Folder;*)
<>ImagePath_Server_t:=$path+"Images"

If (Test path name(<>ImagePath_Server_t)#Is a directory )
   CREATE FOLDER(<>ImagePath_Server_t)
End if


To obtain the path on 4D we want to use the Get_ImagesPath_4D method in the On Startup Database Method with the following condition:

Case of
   : (Application type=4D Local Mode )
      Get_ImagesPath_4D
End case


   `..............................................................
   ` Project Method: Get_ImagesPath_4D
   ` Description: gets the 4D path to Images folder
   ` Parameters: None
   ` Created by luis pineiros
   ` 2009
   `..............................................................


C_TEXT($path)
C_TEXT(<>ImagePath_4D_t)

$path=Get 4D folder(Database Folder;*)
<>ImagePath_4D_t:=$path+"Images"

If (Test path name(<>ImagePath_4D_t)#Is a directory )
   CREATE FOLDER(<>ImagePath_4D_t)
End if


Now we have our path whether the application is running on Client-Server or 4D Single User. All that is left is to create a dialog to put the Image file in a variable (vLoadImage_p). You can make the variable drag and drop and even check for the size and characteristics of the image. Once you have the image, you can save it in its original format or use the WRITE PICTURE FILE command to change its format before saving it.

A Save button on the dialog with the Upload_Image method completes the process.

   `..............................................................
   ` Project Method: Upload_Image
   ` Description: copies image to Images folder
   ` Parameters: None
   ` Returns: Nothing
   ` Created by luis pineiros
   ` 2009
   `..............................................................


C_TEXT($ImageName_t;$locPath_t;$Sep_t)
C_PICTURE(vLoadImage_p)
C_LONGINT($platform_l)

PLATFORM PROPERTIES($platform_l)

If ($platform_l=Windows )
   $Sep_t:="\\"
Else
   $Sep_t:=":"
End if

   `You can use the original Image name or rename it
   `If you use the original name you should obtain it
   `when you open the image in the dialog

$ImageName_t:="Image_0001"

Case of
   : (Application type=4D Server )
      $locPath_t:=<>ImagePath_Server_t+$Sep_t+$ImageName_t
   : (Application type=4D Remote Mode )
      $locPath_t:=<>ImagePath_Server_t+$Sep_t+$ImageName_t
   : (Application type=4D Local Mode )
      $locPath_t:=<>ImagePath_4D_t+$Sep_t+$ImageName_t
End case

   `Write the image as a JPEG
WRITE PICTURE FILE($locPath_t;vLoadImage_p;"JPEG")