KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Using the RECEIVE PACKET command with binary data such as ZIP of PDF files
PRODUCT: 4D | VERSION: 13.0 | PLATFORM: Mac & Win
Published On: March 30, 2012

In order to use the RECEIVE PACKET command with binary data (like a PDF, ZIP, or Image file) the variable you are receiving into needs to be of binary type (C_BLOB).

With that in mind, the following code will not work properly with binary files:

C_TEXT($temp)
C_TIME($docRef)
C_LONGINT($size)
$docRef:=Open document("";Read Mode)
If (OK=1)
 If (Test path name(Document)=Is a document)
  $size:=Get document size(Document)
  If ($size>0)
   RECEIVE PACKET($docRef;$temp;$size)
   If (OK=1)
    ALERT("OK")
   Else
    ALERT("failed to receive packet")
   End if
  End if
 End if
 CLOSE DOCUMENT
($docRef)
End if

When using the code above with a binary file (for example, a PDF file) the OK variable is set to 0 after the RECEIVE PACKET command.

However, the following code should work with both text and binary files such as PDF, ZIP, and image files:
C_BLOB($temp)
C_TIME($docRef)
C_LONGINT($size)
$docRef:=Open document("";Read Mode)
If (OK=1)
 If (Test path name(Document)=Is a document)
  $size:=Get document size(Document)
  If ($size>0)
   RECEIVE PACKET($docRef;$temp;$size)
   If (OK=1)
    ALERT("OK")
   Else
    ALERT("failed to receive packet")
   End if
  End if
 End if
 CLOSE DOCUMENT
($docRef)
End if


The only difference between the two sets of code is the declaration of $temp from C_TEXT to C_BLOB