NetTalk Central

Author Topic: Generating a file, on request, and sending that.  (Read 9561 times)

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Generating a file, on request, and sending that.
« on: May 12, 2010, 10:42:33 PM »
In some cases the user clicks on a link to get a "page", but in reality that page is generated on the fly, served to the user, then possibly deleted.

The most common instance of this is with a report, and in the case of a report it's all done for you by the report extension template. However it's equally straightforward to generate any file in this way. It might be a PDF, or any other kind of file.

1. Create a new NetWebPage procedure. This will be the URL that the user uses to ask for this document.

2. Set the "Page Type" of the procedure to "FILE"

3. Set the FileName of the procedure to some local variable, (which you need to declare) - for example
loc:filename

4. In the "Before Header" embed point add your code to generate the file*. Note that you should set the loc:filename variable here to contain the name of the file you are generating.
 
5. (optional). The page will attempt to determine the correct Content-type header based on the extension of the file you create. So if you create a file with a pdf extension it'll set the content type as if it's a pdf file. You can override this property if you need to in the "Processed Code" embed, priority 1200.

6. In the same place as 5, set the Content-Disposition of the file you are sending, if you do not want (or not expect) the browser to be able to display the file. For example, the following line tells the browser to display the "save as" screen, and supplies the default file name.

  p_web.HeaderDetails.ContentDisposition = 'attachment; filename="'&clip(loc:fileName)&'"'

7. (optional). If you want to delete the file after it has been sent, then add code to the Processed Code embed, priority 9500.

Example 50 (File Download) is a good example to study if you want to do this sort of thing.

* In step 4 you add the code to generate the file. If this process is slow (ie takes more than 30 seconds) then you need to keep the browser "interested" while you are taking your time to generate. The way to do this is to add calls to

  p_web.NoOp()

during the generation process. You can (and should) call this method inside loops etc. Don't worry about over-doing it, the method itself is smart enough  to cope with that.

cheers
Bruce



[attachment deleted by admin]
« Last Edit: May 12, 2010, 10:47:01 PM by Bruce »