I use file upload in Embperl without problems, can you post your form
and describe better (with code) the way you are handling %fdat to
retrieve the file?
oh boy. this is going to get nasty quickly. i'll try to a few code sources without showing too much since the way i'm building my site is kinda complex in structure. hopefully it'll be enough.

the main file containing the embperl form page is the one below. this file mainly is used to capture the data and then send it off to the index.epl page which contains the code for calling handler/controller object for manipulating the %fdat and %udat variables. this page is just a view with a few elements for displaying the results of either a lookup or after the form is submitted for editing purposes.

<form method="POST" enctype="multipart/form-data">
<table border="0" width="100%" cellpadding="1" cellspacing="0">
<tr class="outer">
<td>
<table border="0" width="100%" cellpadding="2" cellspacing="1">
<tr class="inner">
<th colspan="2">
Add/Edit Image
</th>
</tr>
[$ if $fdat{msg}->{msg} $]
<tr class="error">
<td align="center" colspan="2">
[+ $fdat{msg}->{msg} +]
</td>
</tr>
[$ endif $]
[$ if defined $fdat{msg}->{data}->{image}->{id} $]
<tr class="inner">
<td colspan="2" align="center">
<img src="[+ $fdat{msg}->{data}->{image}->{url} +]"
width="[+ $fdat{msg}->{data}->{image}->{width} +]"
height="[+ $fdat{msg}->{data}->{image}->{height} +]">
</td>
</tr>
[$ endif $]
<tr class="inner">
<td width="50%" align="right">
Load Image:
</td>
<td width="50%" align="left">
<input type="file" name="upload">
</td>
</tr>
<tr class="inner">
<td align="right">
Image Type:
</td>
<td>
[-
$fdat{image_type_id} =
$fdat{msg}->{data}->{image}->{image_type_id}
if $fdat{msg}->{data}->{image}->{image_type_id};
Execute('image_type_select.epl');
-]
</td>
</tr>
<tr class="inner">
<td width="50%" align="right">
Height:
</td>
<td width="50%" align="left">
<input type="text" name="height" value="[+ $fdat{msg}->{data}->{image}->{height} +]">
</td>
</tr>
<tr class="inner">
<td align="right">
Width:
</td>
<td>
<input type="text" name="width" value="[+ $fdat{msg}->{data}->{image}->{width} +]">
</td>
</tr>
<tr class="inner">
<td colspan="2" align="center">
<input type="submit" name="act::insert" value="Add" onClick="return checkAdd()">
[$ if defined $fdat{msg}->{data}->{image}->{id} $]
<input type="submit" name="act::update" value="Update" onClick="return checkEdit()">
[$ endif $]
</td>
</tr>
</table>
</td>
</tr>
</table>
<input type="hidden" name="id" value="[+ $fdat{msg}->{data}->{image}->{id} +]">
<input type="hidden" name="e" value="add_image">
</form>

this next page is the controller kind of view which makes calls to instantiate a controller object and hand the request and session information to the controller object (as well as some customized environment settings which is represented by the
$Apache::system->{config} and $Apache::ep references).


[-

use Handler::Controller;

my $controller = new Handler::Controller(
system => $Apache::system->{config},
event => $Apache::ep,
session => \%udat,
request => \%fdat
);
$fdat{msg} = $controller->process();
if (defined $fdat{msg}->{data}->{dude})
{
$udat{user} = $fdat{msg}->{data};
}
-]
<html>
<head>
</head>
<body>
[- Execute ('header.epl') -]
<p>
[- Execute ($fdat{msg}->{page}) -]
</p>
</body>
</html>

the next object is handles the request data and assembles it into hash refs (or occasionally array refs depending on the application). I do this to segregate my data so that I can send it to an appropriate data object for persisting in my database. However, you'll note here that there is a piece of code that says:

upload => $q->{upload} which represents the uploaded file.

package Builder::AddImage;

use Builder::Base;
use strict;

@Builder::AddImage::ISA = qw(Builder::Base);

sub init
{
my ($self, $class, %args) = @_;
$self->{image} = {};
}

sub build
{
my $self = shift;
my $q = $self->{request};
$self->{image} = {
id => $q->{id} || 0,
image_type_id => $q->{select_image_type},
height => $q->{height},
width => $q->{width},
upload => $q->{upload},
};
return 1;
}

1;

the file that writes the file to my server is the one below. i think it's pretty close to how a lot upload scripts handle saving a file to the server, except that i use an object to handle it.

package Utilities::Upload;

use strict;

sub new
{
my ($class, %args) = @_;
my $self = {};
$self->{file} ||= $args{file};
$self->{dest} ||= $args{dest};
bless $self, ref $class || $class;
}

sub upload
{
my $self = shift;
my $file = "$self->{dest}/$self->{file}";
unlink $file if -e $file;
open FILE, ">>$file" || die $!;
my $buffer;
my $totallength = 0;
my $bytes;
while( $bytes = read($self->{file}, $buffer, 1024) )
{
print FILE $buffer;
$totallength += $bytes;
}
close FILE;
close $self->{file};
}

1;

anyway, there's a few pieces i'm leaving out like the controller object itself, the system configuration and event processors, and the business logic modules (this is just for the sake of brevity). in this case though, i don't think they're necessary to show because the %fdat variable doesn't really make it to the objects at those levels. however, i did notice something about what happens when you do a post and multiform together. it seems that there are two keys that result in the %fdat hash which are " name" (note the space) and " filename" which contains the filename and the contents of the file. In the " name" key, you'll see the remaining key/value pairs. i'm curious why this occurs in this situation. are you supposed to parse the %fdat in a "special" manner when handling file uploads? what if you, for instance, you have multiple input fields that you want to submit? is there a standardized mechanism for extracting them?

thanks!


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to