download example


Using CobolScript to Transmit Files

Within HTML, you can provide links to files that can be downloaded by using the anchor tag (<A HREF= … >), but if you do this your users will be able to see the location of the file on your server when they view your HTML source. If you want to hide the location of your files and regulate who downloads files from your site, you can build a CobolScript program to directly send the file to the user’s web browser.


CobolScript can be used to send a file to a client web browser. This is accomplished by sending the appropriate MIME header and then using either the DISPLAYFILE or DISPLAYASCIIFILE commands, depending on whether the file is binary or ASCII text. The user will be presented with a “Save As…” dialog box like the one in Figure 7.5, and will be allowed to save the file.

To use DISPLAYFILE or DISPLAYASCIIFILE, you should first build a program that displays a form that a user will submit when he wants to download a file. Within this form, specify the CobolScript program that will use the appropriate command to transmit the file. Typically this form will contain a submit button, and possibly some additional fields that you will use to validate the user, as in the following:

<FORM ACTION=”/cgi-bin/cobolscript.exe?down.cbl” METHOD=”POST”>

<INPUT TYPE=”hidden” NAME=”user_id” VALUE=”md837653 “>

<INPUT TYPE=”hidden” NAME=”password_id” VALUE=”83fFrR “>

<INPUT TYPE=”hidden” NAME=”file” VALUE=”budgetfile “>

<INPUT TYPE=”Submit” VALUE=”Download”>

</FORM>

When this form is submitted, it will run the program you specify in the ACTION attribute of the FORM tag (down.cbl in this example). Your program can then accept authentication information and decide whether to transmit the file to that particular user based on this information. If you choose to not send the file, you can simply display an error page instead.

After you have validated the authentication information, you can begin transmitting the file to the user. There are two steps to this process. First, you will need to display a special MIME header. This mime header is what prompts the user’s web browser to show the “Save As” dialog box. The file name that you use in your MIME header will be the default file name in the “Save As” dialog box. It is very important that the file size in your MIME header matches the exact file size of the file you wish to transmit; in bytes. If it doesn’t, your file will not be transmitted correctly to the user.

After you have displayed the appropriate MIME header, you can use the DISPLAYFILE or DISPLAYASCIIFILE statement. This will transmit the contents of the file to the client’s web browser after he selects the “Save” button from the “Save As…” dialog.

Here’s a CobolScript code example with the appropriate MIME header and the DISPLAYFILE statement (DISPLAYASCIIFILE could be substituted for DISPLAYFILE below if the file to be transferred is a text file):

MOVE `budget.xls` to xfer_filename

MOVE `octet-stream` to xfer_method

MOVE 420000 TO xfer_filesize

DISPLAY `Content-type: application/` & xfer_method.

DISPLAY `Content-Disposition: inline; filename=` & xfer_filename.

DISPLAY `Content-Description: ` & xfer_filename.

DISPLAY `Content-Length: ` & xfer_filesize.

DISPLAYLF.

DISPLAYFILE download_filename.

By using this technique, you can regulate downloads, and audit which users download your files. You can also build custom text files that will be sent to your users by displaying a MIME header and then displaying individual lines, one line at a time. If you do this, make certain that the amount of data you send matches the Content-Length specified in your MIME header.