Perl print command to download a file

I am trying to figure out how to do the simple task of downloading a file from a webserver through a Perl cgi script. Do I use the print command?

Uploading is easy : if I use input type "file" like in my perl script I automatically get to browse my harddrive for the file to open.

How can I do this for downloading? something like the below but using print FILE STDOUT ???

open(FILE,">$directory/$filename") || &message("$TEXT{'error'}");

while (read($upload_file,$buff,2096)) {
print FILE $buff;
}

close(FILE);

grateful for any help

Comments

  • : I am trying to figure out how to do the simple task of downloading a file from a webserver through a Perl cgi script. Do I use the print command?
    :
    : Uploading is easy : if I use input type "file" like in my perl script I automatically get to browse my harddrive for the file to open.
    :
    : How can I do this for downloading? something like the below but using print FILE STDOUT ???
    :
    : open(FILE,">$directory/$filename") || &message("$TEXT{'error'}");
    :
    : while (read($upload_file,$buff,2096)) {
    : print FILE $buff;
    : }
    :
    : close(FILE);
    :
    : grateful for any help
    :
    :

    Hi there,

    It depends upon the type of file you're trying to transfer. If it's a text file then use the MimeType text/plain, but if it's binary, you need to print the correct MimeType and set BINMODE STDOUT then print the data to the browser.

    HTH
    Bradley q:)
  • : : I am trying to figure out how to do the simple task of downloading a file from a webserver through a Perl cgi script. Do I use the print command?
    : :
    : : Uploading is easy : if I use input type "file" like in my perl script I automatically get to browse my harddrive for the file to open.
    : :
    : : How can I do this for downloading? something like the below but using print FILE STDOUT ???
    : :
    : : open(FILE,">$directory/$filename") || &message("$TEXT{'error'}");
    : :
    : : while (read($upload_file,$buff,2096)) {
    : : print FILE $buff;
    : : }
    : :
    : : close(FILE);
    : :
    : : grateful for any help
    : :
    : :
    :
    : Hi there,
    :
    : It depends upon the type of file you're trying to transfer. If it's a text file then use the MimeType text/plain, but if it's binary, you need to print the correct MimeType and set BINMODE STDOUT then print the data to the browser.
    :
    : HTH
    : Bradley q:)
    :

    Hi thanks for the response.
    Well, basically I am trying to do the exact opposite of my upload code. With my upload code, a user can go to a website and upload any file from their hard drive - they use the HTML command



    and then a Browse button appears on the page. When Browse is pressed they can upload any file they like to the directory. For this I use

    open(FILE,">$directory/$filename") || &message("$TEXT{'error'}");
    if( $query->param('mod') ne "1" )
    {
    binmode FILE;
    }
    while (read($upload_file,$buff,2096))
    {
    print FILE $buff;
    }
    close(FILE);

    and that's fine. My question is how do I do the exact opposite for downloading? I don't want to print the data to the browser, I want the user to be able to download the file to their hard drive.

    Only code I could find was the below - it doesn't work!

    sub getFile {
    my($file) = @_;

    open(FILE, "$file") or die "Can't open $file: $!
    ";
    print "Content-Type: application/unknown
    ";
    print 'Content-Disposition: attachment; filename=' . basename($file) . "
    ";
    print 'Content-Length: ' . (stat($file))[7] . "
    ";
    print "
    ";

    while () { print; }
    close(FILE);
    }

  • Hi,

    To the best of my knowledge, the way to do this *is* to print the content to the web browser with a header to say what type of file it is. Even then, it's dependent on the way the user has their browser set up - if they have set it to always open certain types of data rather than safe them, their settings will apply.

    There may be some other way, but I can't think of anything right now...

    Jonathan


    -------------------------------------------
    Count your downloads:
    http://www.downloadcounter.com/
    And host your site:
    http://www.incrahost.com/
    Don't say I never give you anything... ;-)

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories