hotsolutions has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to upload files to a <INPUT TYPE=FILE name="file1"> element using WWW:Mechanize.
My files are in memory - I cannot save them to /tmp and then do something like
$mech->submit_form(form_name => 'something', fields => { file1 => '/tm +p/myfile' }
so - is there any way to pass the file as an in-memory object? I can do it with something like this:
my $input = $m->current_form()->find_input('file1'); $input->content( $content_here ); $input->filename( $filename ); $input->headers( content_type => $content_type );
but how would I determine the content type for any type of file?

Replies are listed 'Best First'.
Re: Upload in-memory files with WWW:Mechanize
by BrowserUk (Patriarch) on Mar 05, 2009 at 04:43 UTC
Re: Upload in-memory files with WWW:Mechanize
by kyle (Abbot) on Mar 05, 2009 at 04:15 UTC

    You can use File::Type to get the type of a file in memory.

Re: Upload in-memory files with WWW:Mechanize
by Anonymous Monk on Mar 05, 2009 at 04:39 UTC
    See HTML::Form
    my $upin = $mech->current_form()->find_input('upload'); $upin->content('the content'); $mech->submit();
Re: Upload in-memory files with WWW:Mechanize
by ikegami (Patriarch) on Mar 05, 2009 at 04:29 UTC
    Does the receiver even care? WWW::Mechanize wouldn't have set it (right?), so you're going beyond your stated goal.