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

Hi monks I am writing a file to upload a picture.Here is what I should send to the server Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip,deflate Connection: keep-alive X-Requested-With: XMLHttpRequest X-File-Name: fun.jpg Content-Type: application/octet-stream Content-Length: 63970 Pragma: no-cache Cache-Control: no-cache ############ raw image ###########
here is what I have so far:
my $browser = LWP::UserAgent->new; $picture = 'fun.jpg'; $browser->default_header('X-Requested-With' => "XMLHttpRequest"); $browser->default_header('X-File-Name' => $picture); my %args; my $field_name = ""; my $buf ; my $buf_ref = $args{'buf'} || \$buf ; my $value = read_file( $picture , binmode => ':raw' , scalar_ref => 1 +); my $response = $self->browser->post( " +c/?qqfile=".$picture, Content_Type => 'application/octet-stream', Content => $value ); print $response->content;
I can not send the image as raw data. Please help

Replies are listed 'Best First'.
Re: lwp post file
by roboticus (Chancellor) on Dec 23, 2012 at 13:29 UTC

    Okay, I've had my morning coffee, now.

    What error messages are you getting? What are your symptoms? What is the purpose of lines 6 through 9? Which modules are you using?

    Usually, I'd suggest adding warnings and strict and then fixing your errors. In this case, I'll suggest fixing your errors, and then adding strict and warnings and fix the new ones that come up.


    When your only tool is a hammer, all problems look like your thumb.

    This code doesn't look like you seriously tried at all. You don't specify your modules, you set up variables you don't need, it makes little to no sense. It looks like a handful of cargo-culted lines from somewhere. On top of that, you want to figure out how to post something to a website? I suggest you spend a little time learning the basics of the language before trying to do this project.

    In the event that you do know perl, then you sure chose a poor way of stripping down your code. You stripped out your use declarations, but you left in some lines that do *nothing* to further your task.

    I hate to be bitchy, but It's a fundamental skill of debugging to be able to reduce your code to a small example to reproduce a bug. Another fundamental skill is to describe the problem. You can't send the image as raw data? Well, *what are the symptoms*? Blue smoke? A genie slapping your hand with a ruler? The Internet police issuing you a citation for "bad content uploading"?

      line 6-9 read file (image)

        No, line 10 does that. Lines 6 through 9 do nothing--in the code shown.

        Update: Line 10 *almost* does that. The $picture variable isn't defined....


        When your only tool is a hammer, all problems look like your thumb.

Re: lwp post file
by negrotelmonk (Initiate) on Jan 15, 2013 at 19:40 UTC

    here is the correct code for ajax img upload

    my $browser = LWP::UserAgent->new; $picture = 'fun.jpg'; $browser->default_header('X-Requested-With' => "XMLHttpRequest"); $browser->default_header('X-File-Name' => $picture); my $value = read_file( $picture, binmode => ':raw', scalar_ref => 0); my $response = $self->browser->post( " +c/?qqfile=".$picture, Content_Type => 'application/octet-stream', Content => $value ); print $response->content;