KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Creating an HTTP Download Procedure
PRODUCT: 4D | VERSION: 2003.3 | PLATFORM: Mac & Win
Published On: June 10, 2004

Versions: 6.8.x and 2003.x

To send a document to a Web browser, you need to execute the command SEND HTML BLOB. This requires that you pass a specific document type as a parameter. Depending on the type of the document, the Web browser may prompt the user to download the document locally or display the document right on the browser.

In case you want all files to be downloaded locally on the client machine, you can use the following method:

` Project Method: GetFileFromServer
` Description: Send a document to a browser and force the browser to save it locally
`
` $1 - TEXT - File Name
` $2 - BLOB - Blob containing a file

C_TEXT($1;$FileName;$header)
C_BLOB($2;$documentBlob)
$FileName:=$1
$documentBlob:=$2
$header:="Content-disposition: attachment; filename="+$FileName
SET HTTP HEADER($header)
SEND HTML BLOB($documentBlob;"application/octet-stream")

The method GetFileFromServer sends a blob that contains a file to the browser and prompts the user to save the file locally.

Here is a scenario where the above method can come in handy. Suppose you have a table [Documents] that has two fields: [Documents]Name (Alpha) and [Documents]xDocument (Blob). [Documents]Name contains the name of a file and [Documents]xDocument contains an actual file.

You can create or modify the On web Connection method so that it extracts the name of the file from the URL, performs a query in the database, and executes the method GetFileFromServer.

Here is an example of what the On Web Connection should look like:
` Database Method: On Web Connection
C_TEXT($1;$2;$3;$4;$5;$6)
C_TEXT($filename)
$filename:=Substring($1;8)   ` Extract file name from the request
QUERY([Documents];[Documents]Name=$filename)   ` Search for the requested file in the database
If (Records in selection([Documents])>0)
GetFileFromServer ([Documents]Name;[Documents]xDocument)
Else
       ` Error Handler
End if

To make a request from a Web browser, the URL must be in the following format:

http://www.domain.com/4dcgi/filename.ext

e.g.

http://www.domain.com/4dcgi/4D Language Ref.pdf
http://www.domain.com/4dcgi/Report.doc
http://www.domain.com/4dcgi/Image.jpg