Approaches to Web Development for Bioinformatics
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:
use CGI;
my $query =
new CGI;
print
$query->header,
$query->start_html('File Upload Example'),
$query->h1('File Upload Example');
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;
}
else {
my
$filename = $query->upload(
'uploaded_file');
my
$type = $query->uploadInfo($filename)->{
'Content-Type'};
print
"File uploaded $filename, type $type",
$query->br;
if ($type eq
'text/plain') {
print
"File Contents (or first five lines): ", $query->br;
my $i =
0;
while (
defined(
$line = <$filename>) && ($i <
5)) {
print $line, $query->br;
$i++;
}
}
else {
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.
There are no user comments.
Please send ideas and opinions by email at alexamies@gmail.com.
© 2006-2007 Alex Amies