I have written a script for retrieving the records from the pgsql database then create a text file(records.tx t) and save it on the server, after that attach that file in order to email to the receiptants base on users request. What I want to enhance the system is that instead of emailing to a particular receiptant, I'd like to develop a .html file for users to set the condition for retrieving the records and provide a function to download the records in either .txt file or .html file. First step of enhancing the system, I stuck up in the beginning part of download script. So that I would appreciate if you help me out with giving a sample script for file downloading. Just a simple one will be fine. Thank you in advance!!!
downloading a file from the server using .pl
Collapse
X
-
You can just make an HTML link to it as your file sits on the server but depending on MIME types and stuff it may launch some viewer/app u don't want
and you'd prefer the Save As box to pop
make suer the user gets the download option use the Content-disposition header
....
open(FILY, "<$file_full_pa th") || Error('open', 'file');
@file_dwnld = <FILY>;
close (FILY) || Error ('close', 'file');
......
print "Content-Type:applicatio n/x-download\n";
print "Content-Disposition:att achment;filenam e=$this_file_na me\n\n";
print @dwnld
}
...
Ciao -
causing a file to download is a function of the http headers, not a perl script. But you can use a perl script to print the appropriate headers. The CGI module will handle the appropriate headers and more, even for file downloads/attachments. You can find that information in the "CREATING A STANDARD HTTP HEADER:" section of the CGI module documentation. http://perldoc.perl.org/CGI.html
Or you can print your own headers, something similar to what tifoso posted, although those look a little incomplete.Comment
-
thanks a lot!
here is a working script of mine for those who also got a same prob as mine :) cheers!
Code:my $file_location; my @fileholder; $file_location = "/home/msg/"; my $filename = 'test.txt'; my $dlpath ="/desktop/"; open(DLFILE, "<$file_location/$filename") || Error('open','file'); @fileholder = <DLFILE>; #$dlpath = <DLFILE>; close (DLFILE) || Error ('close','file'); open(LOG,">>/home/msg/logs/testing.log")||Error('open','file'); print LOG "$filename\n"; close(LOG); print "Content-Type:application/x-download\n"; print "Content-Disposition:attachment;filename=$filename\n\n"; print @fileholder;Comment
-
it's nice to add the content-length header:
Code:my @fileholder; my $file_location = '/home/msg/'; my $filename = 'test.txt'; my $dlpath = '/desktop/'; [B]my $filesize = -s "$file_location/$filename";[/B] open(DLFILE, "<$file_location/$filename") || Error('open','file'); my @fileholder = <DLFILE>; close (DLFILE) || Error ('close','file'); open(LOG,">>/home/msg/logs/testing.log")||Error('open','file'); print LOG "$filename\n"; close(LOG); [B]print "Content-length: $filesize\n"; [/B] print "Content-Type:application/x-download\n"; print "Content-Disposition:attachment;filename=$filename\n\n"; print @fileholder;Comment
Comment