-1

Hi I am trying to upload image file using perl cgi-application. Not sure what I am doing wrong - but an empty image file (with the right name) gets saved.


my $picture = $self->query->param('picture') ;
my $buffer; my $bytesread;

open (OUTFILE, ">>$user_dir/profile_picture/$picture");
while ($bytesread = read($picture, $buffer, 1024)) {
print OUTFILE $buffer;
}

prat
  • 607
  • 2
  • 8
  • 22

2 Answers2

3

Read the " CREATING A FILE UPLOAD FIELD" section of CGI.pm documentation. There you will see that the upload method will return a filehandle.

    my $fh = $self->query->upload('picture');

    my $buffer; my $bytesread;
    while ($bytesread = read($fh, $buffer, 1024)) {
      ...
     }
jira
  • 3,890
  • 3
  • 22
  • 32
  • I saw that, the upload method however works only with plain old CGI not with cgi::application – prat Nov 15 '10 at 21:19
  • Since `query()` returns a plain old `CGI` object, it should work just fine. – Quentin Nov 15 '10 at 21:26
  • That's what I expected - but it doesn't. Others have complained about it in forums. its probably a bug in cgi::application. – prat Nov 15 '10 at 23:02
  • Rest assured that there is no bug in CGI::Application affecting file uploads. I use it all the time. There is a bug somwhere in you code or in your HTML/Javascript. Maybe what Aquatoad says. – jira Nov 16 '10 at 10:37
  • I found it. This was what I was missing - binmode OUTFILE; - it handles non-text formats – prat Nov 16 '10 at 17:48
0

Don't forget your HTML form element also needs to specify enctype='multipart/mixed', otherwise the upload filestream doesn't get sent.

Aquatoad
  • 778
  • 8
  • 21