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

Hello monks,

I'm currently working on a little Website where users can upload documents and then download them again etc. So it should of course be as userfriendly as possible, but as default when you want to upload a file, netscape pops up it's explorer-styled dialog with ".html" as default filemask. My question is if it's possible to change that defaultmask to "*.*".

Another thing is that if the Docufiles are .html-files by default when linking to them, of course browser wants to show that files and not download them. I know that it's possible via "save as" context-menu but maybe there's a way to get that dialog-download box by default with content-headers or something?

thanx and have a nice day,

giant___
  • Comment on Upload-dialog and CGI-download possibilities

Replies are listed 'Best First'.
Re: Upload-dialog and CGI-download possibilities
by tachyon (Chancellor) on Apr 11, 2002 at 13:10 UTC
    <a href="http://www.yoursite.com/cgi-bin/download.cgi?filename=somefil +e.txt"> Download somefile.txt </a> #!/usr/bin/perl -wT use strict; use CGI; # clean up the environment for CGI use BEGIN { delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; $ENV{'PATH'} = '/bin:/usr/bin:'; } $CGI::DISABLE_UPLOADS = 1; $CGI::POST_MAX = 1024; my $q = new CGI; my $filepath = '/vs/www.yoursite.com/'; my $filename = $q->param('filename'); # stop backing up path with filname like ../../../etc/passwd invalid($filename) if $filename =~ m|../|; # untaint $filename - allow alphanumerics . - and / in name ($filename) = $filename =~ m|^([\w\d.-/]+)\z|; download_file( $filepath, $filename ); exit; sub download_file { my ($filepath,$filename) = @_; invalid($filename) unless -e $filepath.$filename; my $filesize = -s filepath.$filename; # could use _ print "Content-disposition: attachment; filename=$filename\n"; print "Content-Length: $filesize\n"; print "Content-Type: application/octet-stream\n\n"; my $buffer; open FILE, $filepath.$filename or die "Oops $!"; binmode FILE; binmode STDOUT; print $buffer while read(FILE, $buffer, 4096); close FILE; } sub invalid { my $filename = shift; print $q->header, "<p>$filename does not exist on this server"; exit; }

    This will send a specific file with its name to the browser. A word of warning. First we need to hardcode a $filepath into the script. Second we need to stop someone downloading $filename = '../../../etc/passwd'.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Upload-dialog and CGI-download possibilities
by BUU (Prior) on Apr 11, 2002 at 12:33 UTC
    If you return Content-type:application/octet-stream\n\n it forces a download
      Thanx BUU!

      So I'd have to do another .cgi that's just for passing users the files? and then have "Content-type:application/octet-stream\n\n" there? Would I just have to do an open..etc. print..etc ?

        So I'd have to do another .cgi that's just for passing users the files?

        Possible, but inefficient, and if it's a large site, it will add a lot to your server's load. I'd create a separate directory, and tell my webserver to force the content type for all files in it.

        If you have Apache (1.3), have a look at the ForceType directive.

        Yes, I reinvent wheels.