Ciao a tutti,
Devo permettere il download di un file .ZIP tramite CGI tenendo nascosta l'URL quindi ho provato a creare alcune soluzioni che però non danno il risultato desiderato;
di seguito indico i tentativi ed il loro risultato di seguito, vorrei che qualcuno mi indicasse EVENTUALI ERRORI o altri PROBLEMI che non ho considerato per la mia incompleta conoscenza dell'argomento:
.1.
codice:
#!user/bin/perl
use strict;
use CGI;
my $cgi = new CGI;
my $file_size = (stat "download/file.zip")[7];
print $cgi->header( -type=>'application/octet-stream',
-attachment=>'download/file.zip',
-Content_length=>$file_size);
RISULTATI:
-HTTP:
Internal Server Error 500
-SHELL:
mi Stampa l'HEADER senza problemi
-Dove è l'errore???
.2.
codice:
#!/usr/bin/perl
use strict;
my $size = (stat "download/file.zip")[7];
my $buf ='';
open(DATA,"<download/file.zip");
binmode(DATA);
read(DATA, $buf, $size);
print "Content-Type:application\/octet-stream\n\n";
print $buf;
close(DATA);
RISULTATI
-SHELL:
Stampa l'HEADER e poi il File seguiti dal PROMPT seguito a sua volta dal nome del client Telnet per circa 30 volte consecutive 'PuTTYPuTTYPuTTYPuTTYPuTTYPuTTY....' tra cui 3 o 4 'PuTTY6c'
???
-HTTP:
il Browser Stampa il File senza l'HEADER
- Tra l'altro Potrebbe dipendere dal mio browser???
.3.
Questa è una parte che ho modificato di uno script esistente.
Notare commento iniziale
codice:
#!/usr/local/bin/perl
#Download a file to a client browser (ignoring plugins and helper apps)
use strict;
use warnings;
my $date = '';
my $alert_msg = '';
my $logfile = '';
my $err = '';
my $dlfile = 'download/file.zip'; # download file name
######### get the size of the file
my $size = (stat $dlfile)[7];
######### send the file to the client's browser
select STDOUT;
$| = 1;
print "Content-Type: application\/force-download\nContent-Disposition: attachment\; filename=$dlfile\nContent-Length: $size\nContent-Description: File Downloader 0.2\n\n";
open(FILEIN, $dlfile) or $err = $!;
if ($err) {
&js_alert($alert_msg,1,"exit"); # abort if can't open download file
die;
}
binmode FILEIN if (-B $dlfile); # binary mode if binary file
my $blksize = (stat FILEIN)[11]; # get file system block size
if (!$blksize) {
$blksize = 16384;
}
my($read,$buf,$log_entry);
while ($read = sysread FILEIN, $buf, $blksize) {
if (!defined $read) {
next if $! =~ /^Interrupted/;
if ($logfile) {
open LOG, "+>>$logfile" or $err = $!;
if (!$err) {
$log_entry = "$date - System Read Error - $!\n\n";
print LOG $log_entry;
close LOG;
$logfile = 0;
}
}
&js_alert($alert_msg,1,"exit"); # abort - file read error
die;
}
my $written = 0;
my $offset = 0;
while ($read) {
$written = syswrite STDOUT, $buf, $read, $offset;
if (!defined($written)) {
if ($logfile) {
open LOG, "+>>$logfile" or $err = $!;
if (!$err) {
$log_entry = "$date - System Write Error - $!\n\n";
print LOG $log_entry;
close LOG;
$logfile = 0;
}
}
&js_alert($alert_msg,1,"exit"); # abort - file write error
die;
}
$read -= $written;
$offset += $written;
}
}
######### finished sending the file
exit;
Risultati:
Stesso risultato della soluzione .2. con l'Header diverso come indicato nel codice.
Grazie anticipatamente per l'interessamento e la pazienza,
Ciao,