I have written an upload.tmpl with <input type="file" name="uploadfile">. And link this tmpl to upload.pl. However, when the cgi->param("uploadfile") is passed in the upload.pl, it doesn't seems to work the way it is supposed to. I think it's only passing as string instead of a filehandle. I tried it without the HTML::Template and it works fine. But when I used the template, it just doesn't work.
The script for upload.pl is as follow:
#!/usr/bin/perl -w
# upload.pl
use strict;
use CGI;
use HTML::Template;
my $cgi = CGI->new();
my $template = HTML::Template->new(filename => "upload.tmpl", path => "/var/www/html/");
my $filename = $cgi->param("uploadfile");
$filename =~ s/.*[\\/\\](.*)/$1/;
my @text = <$filename>;
my $text = join(",", @text);
Thanks a lot Sam. I did put it in my original file. I must have missed it when I put it here. I went back to check again, it was a typo mistake that I've made. Instead of "enctype", I've typed "enctpye". Sorry for the false alarm. Thanks again.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
One thing I don't get is why it worked when he just used it as an HTML webpage. The form enctype seems to have become an issue after he made it a template.
Arka
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have written an upload.tmpl with <input type="file" name="uploadfile">. And link this tmpl to upload.pl. However, when the cgi->param("uploadfile") is passed in the upload.pl, it doesn't seems to work the way it is supposed to. I think it's only passing as string instead of a filehandle. I tried it without the HTML::Template and it works fine. But when I used the template, it just doesn't work.
The script for upload.pl is as follow:
#!/usr/bin/perl -w
# upload.pl
use strict;
use CGI;
use HTML::Template;
my $cgi = CGI->new();
my $template = HTML::Template->new(filename => "upload.tmpl", path => "/var/www/html/");
my $filename = $cgi->param("uploadfile");
$filename =~ s/.*[\\/\\](.*)/$1/;
my @text = <$filename>;
my $text = join(",", @text);
$template->param(text => $text);
print $cgi->header();
print $template->output();
========================================================================================
The upload.tmpl is as follows:
<html lang='en'>
<head>
</head>
<body>
<table>
<tr>
<td>
<form method="post" action="/cgi-bin/upload.pl">
<input type="file" name "uploadfile" size="40" />
<input type="submit" value="upload" />
</form>
</td>
</tr>
<tr>
<td><!-- tmpl_var="text" --></td>
</tr>
</table>
</body
</html>
========================================================================================
Appreciate the help!! Thanks.
You must set the form enctype to "multipart/form-data" for file uploads to work:
<form method="post" action="/cgi-bin/upload.pl" enctype="multipart/form-data">
I really wish browsers would produce a warning about this, it's bitten me a few times too.
-sam
Thanks a lot Sam. I did put it in my original file. I must have missed it when I put it here. I went back to check again, it was a typo mistake that I've made. Instead of "enctype", I've typed "enctpye". Sorry for the false alarm. Thanks again.
One thing I don't get is why it worked when he just used it as an HTML webpage. The form enctype seems to have become an issue after he made it a template.
Arka