The method that I use is to create the file in a directory then use javascript to redirect on a form button click that attempts to download it. Once the user finishes the download and clicks another button, I clean up the file(s).
Something to keep in mind. If multiple users are using this application, then there may be file overlap. You will need to make the filename related to the user either by a username (if via .htaccess) or remote ip address.
Sorry for the pseudo code, but I don't know what other work you've done. I'm basing this of an apache 2.0 implementation. I've capitalized the pseudo code for you. I also assume that you are already printing content to a web page before and after this snippet. I am also assuming the file contents are created and pushed to an array called @fileContentForUser. Since you can use CGI.pm or other methods, you would need to update some code here to reflect that.
if (DLBUTTON eq 'DOWNLOADFILE') { if (PEOPLE LOGIN USING .HTACCESS) { $fileName = "$ENV{'REMOTE_USER'}-output.txt"; } elsif (USING REMOTE ADDRESS) { $fileName = "$ENV{'REMOTE_ADDR'}-output.txt"; } open (CONTENT, ">/html/downloads/$fileName"); while (@fileContentForUser) { # PERFORM ANY FORMATTING OR OTHER PER LINE STUFF HERE print CONTENT "$_\n"; } print qq { <script language=javascript><!-- window.open('/downloads/$fileName','_blank','') //--></script> };
The code for the button on the webpage would be:
<input type=button value="DOWNLOAD" onclick="window.open('/cgi-bin/SCR +IPT_NAME?DLBUTTON=DOWNLOADFILE','','')"
if you want to clean up these files that could contain different names, then I would use (not the most efficient, but it works):
opendir (DIR, '/html/downloads/'); @files = grep /-output.txt/, readdir (DIR); closedir(DIR); foreach (@files) { unlink ("/html/downloads/$_"); }
The other issue you may run into is if the user has a file associated in the browser. If they open text files in the browser instead of downloading them to the drive, the contents will simply open inside the browser. The best thing I find is to zip up or tar the file first. Of course this will depend on your end users.
I hope this helps!
Sincerely,
Marc
www.marcbilodeau.com
In reply to Re: Need to Save Files into Windows
by FitTrend
in thread Need to Save Files into Windows
by EchoAngel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |