Approaches to Web Development for Bioinformatics

Previous  Contents  Next
References

Uploading Files

A file upload field is useful in bioinformatics because often files will be saved from a database from GenBank query or another of the major database and then used in web based forms.  An upload field is created with a script in the form


$query->filefield(-name=>'uploaded_file',
-default=>'starting value ignored',
-size=>50,
-maxlength=>80)

where the first parameter is the name of the form element, the second parameter is ignored by most browsers, the third parameter is the size of the form field as displayed on the HTML form, and the forth parameter the maximum length for the full file name (including path).  Here is an example of a Perl CGI script that uploads a file and prints out the first five lines:


#!/usr/bin/perl
# A script to demonstrate how to upload a file

use CGI;

my $query = new CGI;

print
$query->header,
$query->start_html('File Upload Example'),
$query->h1('File Upload Example');

# Display the form
unless ($query->param('uploaded_file')) {

print $query->start_multipart_form,
"Please upload your file ", $query->br,
$query->filefield(-name=>'uploaded_file',
-default=>'starting value ignored',
-size=>50,
-maxlength=>120),
$query->br, $query->br,
$query->submit('Upload'),
$query->end_form;

# Process the form
} else {

# upload the file
my $filename = $query->upload('uploaded_file');

# Get file upload information
my $type = $query->uploadInfo($filename)->{'Content-Type'};
print "File uploaded $filename, type $type", $query->br;

# Only process text files
if ($type eq 'text/plain') {

# Read in text file and print out the first five lines
print "File Contents (or first five lines): ", $query->br;
my $i = 0;
while (defined( $line = <$filename>) && ($i < 5)) {
print $line, $query->br;
$i++;
}

} else {
# Give a warning message for other types of files
print "Only text files permitted", $query->br;
}

}

print $query->end_html;

Once the file has been uploaded it can be read and processed as if the script were invoked from a command line.  The script also checks to see that the file uploaded is a text file before processing it.  You can test the script with file_upload_example.pl.


Previous  Contents  Next
References

Contributed Comments and NotesAdd a comment.

There are no user comments.

Google

Please send ideas and opinions by email at alexamies@gmail.com.

© 2006-2007 Alex Amies