KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to send non-html files using SEND HTML BLOB
PRODUCT: 4D | VERSION: 11 | PLATFORM: Mac & Win
Published On: July 16, 2009
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

Here is a wrapper method for SEND HTML BLOB that will automatically detect the MIME type of the file. If the mime type cannot be detected, the file is sent to the browser with the mime type of "application/x-unknown". The list of mime types was obtained from documentation page for SEND HTML BLOB. It is necessary because SEND HTML FILE only sends HTML files, you can use SEND HTML BLOB to send all kinds of files to the browser.

  ` METHOD: wwwSendFile
  `
  ` Parameters: 1
  `
  ` $1: path to file being sent
  `
  ` Notes: Essentially this is a wrapper SEND HTML BLOB that will
  `        make it easier to send non HTML files with this command


Case of
  : (Count parameters=1)

    C_BOOLEAN($fQuit)
    C_TEXT($1;$request;$tmp;$mime;$extension)
    C_LONGINT($count;$pos)

    $request:=$1
    $tmp:=$request
    $count:=0
    $fQuit:=False
    $mime:=""

    Repeat
      $pos:=Position(".";$tmp;2)
      $tmp:=Substring($tmp;$pos)
      $extension:=$tmp
      Case of
        :($extension=".htm")
            $mime:="text/html"
        :($extension=".html")
            $mime:="text/html"
        :($extension=".shtml")
            $mime:="text/html"
        :($extension=".shtm")
            $mime:="text/html"
        :($extension=".css")
            $mime:="text/css"
        :($extension=".pdf")
            $mime:="application/pdf"
        :($extension=".rtf")
            $mime:="application/rtf"
        :($extension=".ps")
            $mime:="application/postscript"
        :($extension=".eps")
            $mime:="application/postscript"
        :($extension=".hqx")
            $mime:="application/mac-binhex40"
        :($extension=".js")
            $mime:="application/javascript"
        :($extension=".txt")
            $mime:="text/Plain"
        :($extension=".text")
            $mime:="text/Plain"
        :($extension=".gif")
            $mime:="image/gif"
        :($extension=".png")
            $mime:="image/png"
        :($extension=".jpg")
            $mime:="image/jpeg"
        :($extension=".jpeg")
            $mime:="image/jpeg"
        :($extension=".jpe")
            $mime:="image/jpeg"
        :($extension=".jfif")
            $mime:="image/jpeg"
        :($extension=".pic")
            $mime:="image/pict"
        :($extension=".pict")
            $mime:="image/pict"
        :($extension=".tif")
            $mime:="image/tiff"
        :($extension=".tiff")
            $mime:="image/tiff"
        :($extension=".mpeg")
            $mime:="video/mpeg"
        :($extension=".mpg")
            $mime:="video/mpeg"
        :($extension=".mov")
            $mime:="video/quicktime"
        :($extension=".moov")
            $mime:="video/quicktime"
        :($extension=".aif")
            $mime:="audio/aiff"
        :($extension=".aiff")
            $mime:="audio/aiff"
        :($extension=".wav")
            $mime:="audio/wav"
        :($extension=".ram")
            $mime:="audio/x-pn-realaudio"
        :($extension=".sit")
            $mime:="application/x-stuffit"
        :($extension=".bin")
            $mime:="application/x-stuffit"
        :($extension=".z")
            $mime:="application/x-zip"
        :($extension=".zip")
            $mime:="application/x-zip"
        : ($extension=".gz")
            $mime:="application/x-gzip"
        : ($extension=".tar")
            $mime:="application/x-tar"
        Else
          $mime:=""
          $count:=$count+1
          If ($count > 19)
            $mime:="application/x-unknown"
            $fQuit:=True
          End if
      End case

    Until (($mime#"") | ($fQuit=True))

      If ($mime#"")
        C_BLOB($blob)
        DOCUMENT TO BLOB($request;$blob)
        SEND HTML BLOB($blob;$mime)
      End if

  End case


See Also:
Commented by Timothy Penner on July 16, 2009 at 6:09 PM
This wrapper method becomes very useful when using a "web decoy"