KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Saving a document in a Object field record
PRODUCT: 4D | VERSION: 15 | PLATFORM: Mac & Win
Published On: December 3, 2015

The ability of saving a file and/or picture in a record can be done using the fields of blob and picture respectfully. In v15, the Object field can support saving any document type. The method SAVE_DOCUMENT_IN_OBJ_FIELD can save the integrity of the file in a Object field record shown below:

// ---------------------------------------------------------------------------
// Name: SAVE_DOCUMENT_IN_OBJ_FIELD
// Description: Method will take in a Object field pointer with an property
// name to save a file and file type in the Object field record.
//
// Parameter:
// $1 (Pointer) - Field pointer (Object Field)
// $2 (Text) - Property name to be entered in the record
// $3 (Text) - File location of the file to save in the record
// Output:
// $0 (Long Int) - Status of saving the record:
// 0 - Success
// 1 - Not enough parameters
// 2 - Field is not Object field type
// 3 - File specified cannot be found
// 4 - File cannot be opened
// --------------------------------------------------------------------------
C_POINTER($1;$fieldName;$table_ptr)
C_TEXT($2;$propertyName)
C_TEXT($3;$fileLoc)
C_TEXT($encoded_file_text;$type_doc)
C_LONGINT($0;$status;$table_num)
C_TIME($vhDocRef)
C_BLOB($blob_file)

If (Count parameters>=2)
   $fieldName:=$1
   $propertyName:=$2
   $fileLoc:=$3

   $table_ptr:=Table(Table($fieldName)) // table pointer to create a record
   GET FIELD PROPERTIES($fieldName;$fieldType) // Check if it is Object field

   If ($fieldType=Is object) // Check if it is an Object field

      If ($fileLoc="") // Search and open the file
         $vhDocRef:=Open document("")
      Else
         If (Test path name($fileLoc)=Is a document)
            $vhDocRef:=Open document($fileLoc)
         Else
            $0:=4 // File cannot be found
         End If
      End if

         If (OK=1) // File can be opened
            CLOSE DOCUMENT($vhDocRef)
            $type_doc:=Document type(Document)
            DOCUMENT TO BLOB(Document;$blob_file)
            BASE64 ENCODE($blob_file;$encoded_file_text)

            CREATE RECORD($table_ptr->)
            OB SET($fieldName->;$propertyName;$encoded_file_text;"file_type";$type_doc)
            SAVE RECORD($table_ptr->)
            $0:=0 //Success
         Else
         $0:=3 // File cannot be opened
         End if

   Else
    $0:=2 // Not a Object Field
   End if

 Else
  $0:=1 // Not enough parameters
End If


1. An example when a user can select a file:

C_TEXT($property_name)

$property_name:="file"
SAVE_DOCUMENT_IN_OBJ_FIELD (->[Table_1]Field_obj;$property_name;"")


A prompt to select the file:



File saved in the record:





2. An example when a file is specified:

C_TEXT($loc;$property_name)

$loc:=Get 4D folder(Database folder)+"test.jpg"
$property_name:="picture_file"
SAVE_DOCUMENT_IN_OBJ_FIELD (->[Table_1]Field_obj;$property_name;$loc)


File saved in the record:




Note: Mac plattforms may experience incompatibility of file types with the Document Type command.

An example in the record for Mac when saving an incompatible file read from the Document Type command:

"file_type":"\\u0000\\u0000\\u0000\\u0000"