Uploading a file example

Web Programming Tutorial Part 3 - Uploading a Text file


This tutorial describes a technique that can be used to upload a text file from a client web browser without using FTP.



What is <INPUT NAME="myfile" TYPE=FILE>?

Netscape browsers 2.x,3.x,4.x and IE browsers 4.x and 5.x provide support for an HTML INPUT tag with "TYPE=FILE". Example: . When this type of input tag is inside an HTML form, it will show a text box with a Browse button right beside it. After a file is selected and the form is submitted, is is possible to send the contents of the file to the CGI program that is specified in the ACTION portion of the FORM tag. Although this is a very useful feature, you need to be aware of possible security issues related to letting users upload files to your server.



How does it work?

Currently CobolScript does not have a native command for uploading files. Although, if you are uploading text files you can accomplish this with a little programming and the ACCEPT FROM KEYBOARD command. Here is an example that will give you an idea of how to do this. Create a web page with the following HTML, and name it "test.htm". Place it in your web server's directory for HTML documents.

<html>
<head>
<body>
<form action="/cgi-bin/cobolscript.exe?fileupload.cbl" method=post enctype="multipart/form-data">
<input type=file name=uploadfile>
<input type=submit>
</form>
</body>
</html>

Notice that the FORM tag has a special parameter - enctype="multipart/form-data". This is something special that is needed to upload your files from your web browser. Now create a sample data file on your client with the following data. Name this file "test.txt".

1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890

Next, create a file in your /cgi-bin directory named "fileupload.cbl", and paste the following code inside the file.

       01 mime_boundary      pic x(100).
       01 boundary_length    pic 9(2).
       01 buffer             pic x(100).
       main.
           display `Content-type: text/html` & linefeed.
           display `<HTML><BODY><PRE>`.

           accept buffer from keyboard.
           display `[` & buffer & `]`.

           move buffer to mime_boundary.
           perform varying boundary_length from 1 by 1 until mime_boundary(boundary_length:1) = ` `
           end-perform.
           subtract 1 from boundary_length.


           accept buffer from keyboard.
           display `[` & buffer & `]`.

           accept buffer from keyboard.
           perform until buffer(1:boundary_length) = mime_boundary(1:boundary_length)
              display `[` & buffer & `]`
              accept buffer from keyboard
           end-perform.
           display `[` & buffer & `]`.

           display `</PRE></BODY></HTML>`.
           goback.

Now, bring up a web browser on your client machine and type in http://your server/test.htm. You will see an input box and a "Browse" button beside it. Use the browse button to select the "test.txt" you have created. Next, click the Submit button on the form. This will execute the CobolScript program "fileupload.cbl" on your server. You will see the following in your web browser.

[-----------------------------844157489800                                                           ]
[Content-Disposition: form-data; name="uploadfile"; filename="c:\test.txt"                           ]
[Content-Type: text/plain                                                                            ]
[                                                                                                    ]
[1234567890                                                                                          ]
[1234567890                                                                                          ]
[1234567890                                                                                          ]
[1234567890                                                                                          ]
[1234567890                                                                                          ]
[1234567890                                                                                          ]
[1234567890                                                                                          ]
[1234567890                                                                                          ]
[1234567890                                                                                          ]
[1234567890                                                                                          ]
[1234567890                                                                                          ]
[                                                                                                    ]
[                                                                                                    ]
[-----------------------------844157489800--                                                         ]

The first thing that the program does is ACCEPT the first line of data from the web browser. Because we are using the parameter enctype="multipart/form-data" in our ACTION tag, it will be a MIME boundary. This is a unique set of number generated by client's particular the web browser. Next, we determine the length of this string with an inline PERFORM VARYING command. After that we will use an inline PERFORM UNTIL to get the rest of the data that is being sent from the clients web browser. This is the raw multipart/form-data from the web browser. You can use this program as a base for building the interface between your client's web browser and your CobolScript system.