gshank/html-formhandler

[INVALID] Upload-Field functionality on Win-Systems - wrong validation

hoppfrosch opened this issue · 8 comments

My System is StrawberyyPerl (5.12) on Win7X64

I'm trying to use HTML::FormHandler::Field::Upload as described on http://search.cpan.org/~gshank/HTML-FormHandler-0.40027/lib/HTML/FormHandler/Field/Upload.pm

Unless I can select a file from my disc as value for the upload field, I get the message 'File not found for upload field'. But the file definitivly exists - as I can see the file on my filesystem and the file can be selected via fileselectionbox ....

Is HTML::FormHandler::Field::Upload supposed to work on Win-systems?

I've never used it on Windows and don't have somewhere I can test it. It
looks like the 'is_real_fh' method in HTML::FormHandler::Field::Upload is
failing. Are you using Catalyst::Request::Upload? What are you passing in
to the form for the 'file' param?

Gerda

On Mon, Aug 12, 2013 at 7:15 AM, hoppfrosch notifications@github.comwrote:

My System is StrawberyyPerl (5.12) on Win7X64

I'm trying to use HTML::FormHandler::Field::Upload as described on
http://search.cpan.org/~gshank/HTML-FormHandler-0.40027/lib/HTML/FormHandler/Field/Upload.pm

Unless I can select a file from my disc as value for the upload field, I
get the message 'File not found for upload field'. But the file definitivly
exists - as I can see the file on my filesystem and the fiel can be
selected via fileselectionbox ....

Is HTML::FormHandler::Field::Upload supposed to work on Win-systems?


Reply to this email directly or view it on GitHubhttps://github.com//issues/48
.

Are you using Catalyst::Request::Upload?

At least not explicit, i.e I don't require/use it directly within my Catalyst-application.

What are you passing in to the form for the 'file' param?

I don't understand completely what you are asking for - therefore I give you a few infos which might help.

My form definition has the following attribute for the file parameter:

package BDBInfo2::Form::Additionals::Upload;
use HTML::FormHandler::Moose;

extends 'HTML::FormHandler::Model::DBIC';
with 'HTML::FormHandler::Render::Table';


has_field 'fileAdditional' => (
  label    => 'Hochzuladenes Additional',
  type     => 'Upload',
  max_size => '20000000',
  apply    => [ {
        check   => qr/\A.*\.zip\z/i,
        message => 'Fileextension is not valid (has to be <*.zip>)'
    }],
  required => 1,
);
...

Within my catalyst controller I do have following:

sub upload : Chained("base") : PathPart("upload") : Args(0) {
  my ( $self, $c ) = @_;
  $c->stash(
    template => 'additionals_upload.tt',
    form     => $self->upload_form
  );

  my $success = $self->upload_form->process(
      schema => $c->model('BDB')->schema,
      params => $c->req->parameters,
  );
  ...
 }

I'm using Template Toolkit for rendering.

[% WRAPPER pageframe.tt title = c.config.name ;%]
[% META title = 'Upload Additional' -%]
[% form.render -%]
[% END %]

Running the catalyst application and going to the file-upload page, a button is offered to select the file.
(PseudoImage: (with Button and displayed text))

+-----------+
+ Open .... + No file selected
+-----------+

Clicking this button, a standard windows fileselection box opens, and I select an existing file from my disc. After closing the fileselection box, the filename is shown after the button - only filename is shown (without any path)

+-----------+
+ Open .... + filename.zip
+-----------+

After pressing the submit-Button the error occurs:

+-----------+
+ Open .... + No file selected. File not found for upload field
+-----------+

If you look at the documentation for HTML::FormHandler::Field::Upload, that
field class requires that you pass in a Catalyst upload object or a file
handle in a param for the upload field name. You aren't doing that. The
Upload field doesn't magically know what the file is. You have to tell it.

Gerda

On Wed, Aug 14, 2013 at 8:54 AM, hoppfrosch notifications@github.comwrote:

Are you using Catalyst::Request::Upload?

At least not explicit, i.e I don't require/use it directly within my
Catalyst-application.

What are you passing in to the form for the 'file' param?

I don't understand completely what you are asking for - therefore I give
you a few infos which might help.

My form definition has the following attribute for the file parameter:

package BDBInfo2::Form::Additionals::Upload;
use HTML::FormHandler::Moose;

extends 'HTML::FormHandler::Model::DBIC';
with 'HTML::FormHandler::Render::Table';

has_field 'fileAdditional' => (
label => 'Hochzuladenes Additional',
type => 'Upload',
max_size => '20000000',
apply => [ {
check => qr/\A..zip\z/i,
message => 'Fileextension is not valid (has to be <
.zip>)'
}],
required => 1,
);
...

Within my catalyst controller I do have following:

sub upload : Chained("base") : PathPart("upload") : Args(0) {
my ( $self, $c ) = @_;
$c->stash(
template => 'additionals_upload.tt',
form => $self->upload_form
);

my $success = $self->upload_form->process(
schema => $c->model('BDB')->schema,
params => $c->req->parameters,
);
...
}

I'm using Template Toolkit for rendering.

[% WRAPPER pageframe.tt title = c.config.name ;%]
[% META title = 'Upload Additional' -%]
[% form.render -%]
[% END %]

Running the catalyst application and going to the file-upload page, a
button is offered to select the file.
(PseudoImage: (with Button and showed text))

+-----------+

  • Open .... + No file selected
    +-----------+

Clicking this button, a standard windows fileselection box opens, and I
select a existing file from my disc. After closing the fileselection box,
the filename is shown after the button - only filename is shown (without
any path)

+-----------+

  • Open .... + filename.zip
    +-----------+

After pressing the submit-Button the error occurs:

+-----------+

  • Open .... + No file selected. File not found for upload field
    +-----------+

Maybe windows path of the file is lost?


Reply to this email directly or view it on GitHubhttps://github.com//issues/48#issuecomment-22633113
.

Hmmmm ... obviously I did not paste all my code - sorry about that. Within the controller I do have following:

sub upload : Chained("base") : PathPart("upload") : Args(0) {
    my ( $self, $c ) = @_;

    $c->stash(template => 'additionals_upload.tt',form => $self->upload_form);

    my $success = $self->upload_form->process(schema => $c->model('BDB')->schema, params => $c->req->parameters);

    if ( !$success ) {
        print "***********ERROR*********\n";
        print Dumper( $self->upload_form->errors );
        return;
    }

    print Dumper( $c->req->upload('fileAdditional') );

    if ( my $upload = $c->request->upload('fileAdditional') ) {
        my $filename = $upload->filename;
        print "*****************" . $filename . "*****************\n";
        my $target   = "C:/tmp/upload/$filename";

        unless ( $upload->link_to($target) || $upload->copy_to($target) ) {
            die("Failed to copy '$filename' to '$target': $!");
        }
    }

    return;
}

Does the line

if ( my $upload = $c->request->upload('fileAdditional') ) {

not use a Catalyst::Request::Upload object?

What I see within my log-output:

***********ERROR*********
$VAR1 = 'File not found for upload field';
[2013/08/15 08:41:50] [catalyst] [INFO] *** Request 33 (0.532/s) [6764] [Thu Aug 15 08:41:50 2013] ***
[2013/08/15 08:41:50] [catalyst] [DEBUG] Path is "/additionals/upload"
[2013/08/15 08:41:50] [catalyst] [DEBUG] "POST" request for "additionals/upload" from "172.16.18.26"
[2013/08/15 08:41:50] [catalyst] [DEBUG] Body Parameters are:
.-------------------------------------+--------------------------------------.
| Parameter                           | Value                                |
+-------------------------------------+--------------------------------------+
| repoDest                            | \\mycomp\Download\Packages\External  |
| submit                              | Upload                               |
| userComment                         | jok                                  |
| userIssue                           | BMS-123                              |
'-------------------------------------+--------------------------------------'
[2013/08/15 08:41:50] [catalyst] [DEBUG] File Uploads are:
.--------------+----------------------------+--------------------+-----------.
| Parameter    | Filename                   | Type               | Size      |
+--------------+----------------------------+--------------------+-----------+
| fileAdditio- | AhkDll_Help_EN.zip         | application/x-zip- | 116066    |
| nal          |                            | -compressed        |           |
'--------------+----------------------------+--------------------+-----------'

Where is the path of the file to upload gone to?

The output shows, that the error already occurs with

my $success = $self->upload_form->process(schema => $c->model('BDB')->schema, params => $c->req->parameters);

where $success is not true. I don't know how to tell the process method, that any of the parameters should be a Catalyst::Request::Upload object ...

Sorry for bothering ... Any more guess? As this is currently a (minor) showstopper for me - I have to start to look for an alternative solution else.

Sorry for not responding earlier. You need to read the documentation for
HTML::FormHandler::Field::Upload (
https://metacpan.org/module/HTML::FormHandler::Field::Upload). It's
necessary to pass the upload object in to the upload field in the params
hash:

my $form = My::Form::Upload->new;
my @params = ( file => $c->req->upload('file') )
if $c->req->method eq 'POST';
$form->process( params => { @params } );

You're not doing that that I can see. Note that you also need to include
any other parameters, such as $c->req->parameters.

Gerda

On Fri, Aug 23, 2013 at 3:14 AM, hoppfrosch notifications@github.comwrote:

Sorry for bothering ... Any more guess? As this is currently a (minor)
showstopper for me - I have to start to look for an alternative solution
else.


Reply to this email directly or view it on GitHubhttps://github.com//issues/48#issuecomment-23147276
.

Thank you so much for your patience - your latest comments suggested exactly what I needed and it works now!
(thought I've read the documentation carefully - but obviously I didn't, or at least kave not understood what I've read ... :$ )

One last question: How can I validate on my "upload page" (where I select the file to upload) that the name of the selected file meets certain criteria (for example fileextension eq".zip")?

What I tried:
has_field 'file' => (
type => 'Upload',
max_size => '20000000',
apply => [ {
check => qr/\A..zip\z/i,
message => 'Fileextension is not valid (has to be <
.zip>)'
}],
required => 1,
);

But as "file" is a not a simple string, this test fails ...

Nevermind - found it:

added a validate()-method to my form according to HTML::FormHandler::Manual::Errors ....

Thanks again for resolving my issue!