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

Hello Monks,

I pray thee impart thy wisdom upon me.

I'm using Perl to control the downloading of files from my site. However, I can't get the size of files to show up during the download (e.g. the Firefox download manager says the file is "unknown size"). I assume this is done through HTTP headers, and here's what I'm trying to no avail:

print "Content-Type:application/octet-stream\n"; print "Content-Disposition:attachment;filename=$file;size=$size\n\n"; print @container; # The array holding my file

What am I doing wrong here?

Thanks,
Rob

Replies are listed 'Best First'.
Re: Display file size during download
by ikegami (Patriarch) on Jun 06, 2008 at 02:21 UTC
    It surely uses the value from the HTTP Content-Length header.
      I thought that might be the case, so I modified my code as follows, also to no avail:
      print "Content-Type:application/octet-stream;name=$file\n"; print "Content-Length:$size\n"; print "Content-Disposition:attachment;filename=$file\n\n"; print @container;
      What else am I missing here?
Re: Display file size during download
by ikegami (Patriarch) on Jun 06, 2008 at 05:08 UTC

    (Oops, this was meant to be in reply to Re^2: Display file size during download, but it's fine here.)

    I don't know. It works for me. For example, the following shows "0 of 10KB at 0.0KB/sec; 02:45:45 remain".

    #!/usr/bin/perl $|=1; print "Content-Type: application/octet-stream\n"; print "Content-Length: 10000\n"; print "\n"; for (1..10000) { print('a'); sleep(1); }

    Does it work for you?

    The autoflush is not necessary, but the browser needs to be able to see the header before it can display progress. For example, the following also works for me.

    #!/usr/bin/perl print "Content-Type: application/octet-stream\n"; print "Content-Length: 100000\n"; print "\n"; for (1..20) { print('a' x 5000); sleep(10); }
Re: Display file size during download
by Gangabass (Vicar) on Jun 06, 2008 at 04:46 UTC

    You can try to load your file and look into LiveHTTPHeaders (Firefox extension).

    Also may be you using some proxy server (Proxomitron?). Read this about filtering rules in Proxomitron.

Re: Display file size during download
by Khen1950fx (Canon) on Jun 06, 2008 at 04:17 UTC
    Just for fun, I tried this:

    print "Content-Type:application/octet-stream\n"; print "Content-Disposition:attachment; filename:CGI.pm; size:"; print -s '/usr/lib/perl5/5.8.8/CGI.pm', "bytes\n";
Re: Display file size during download
by monarch (Priest) on Jun 06, 2008 at 11:13 UTC
    I wonder if Chunked Transfer Encoding may be causing issues.. Frequently Apache will use this form of encoding for CGI applications, and almost by definition this is a form of encoding to use when the size of the document is unknown.