![]() |
| Frontier Tutorials / Writing an ObjectNotFoundHandler / Mode 3 Example |
If you want to return something that should not be wrapped in the template and returned as an HTML page, you have to stuff the page table yourself. This is the most powerful approach available for ONFH handlers, and the most complicated.
In this example, we will return binary data from a table in the website.
At the top level of your site, create a new subtable named "#download".
Load a .zip file into this table from your disk (preferably a small one). If you don't have a utility for loading binary files, you can download and use the ImportBinaryFile script from my published custom menu.
Change the name of the binary entry to "binary1". (It can be called anything you want, of course; this change simplifies the following descriptions.)
In your #tools table, create a script called "ReturnBinaryObject". It should look like this:
|
|---|
This script does the following:
|
|---|
Get the last resolved address and the remainder of the path from the page table.
Check to make sure that the URL is, in fact, a download URL. We do this by checking that the remainingPath (1) begins with "download/", and (2) has only two directory levels in it. If it is not a download path, we throw a scriptError identical to that thrown by mainResponder.respond in the absence of an #objectNotFoundHandler.
Extract the download entry name. Strip off any suffix (such as ".pdf"), but retain the suffix so that we can verify that any entry we find is of the requested type.
Check for a matching entry. If such an entry is not found, throw a scriptError, as above.
Check that the matching entry is of the requested type. We do this by converting the suffix and the entry's binary type to MIME types, and comparing the MIME types.
Copy the entry out as the response body, and set the content-type and last-modified fields of the reply.
Throw a special scriptError to bypass further mainResponder processing and return our response without further modification.
At the top level of your site, set the value of "#objectNotFoundHandler" to the address of your new "ReturnBinaryObject" script. ReturnBinaryObject is now your objectNotFoundHandler script.
Now go to your browser and load the page "http://localhost/onfhTutorial/download/binary1.zip". Your browser should begin a download of the file; it may ask you how to deal with the downloaded file (it varies from browser to browser). If you "download" the file to disk, you should be able to open or unzip it with your favorite zip utility.
The above description describes how to respond to a file-download URL, but does not address getting the URL into a page for a user to click on in the first place. See the ONFH Resources page for information on an available tool that handles both sides of this equation.