Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file download script messes up when using save as.. dialog box

Status
Not open for further replies.

mab4311

Programmer
Jan 29, 2008
1
US
hello,
I wrote a perl script to down load files from my website (I pieced it together from what i found on the net). the script does three things: opens a save as... dialog box for selecting destination, updates a log txt file and then writes a thank you html page. the problem is that when I click save on the dialog box it saves OK but then adds the html code to the end of the file rather than sending the html code to the browser. here is my script:

Thanks,
Michael


#!/usr/bin/perl -w

use CGI qw:)standard);

&download_data;
&update_txt;
&print_thankyou;

#----------------------------------------------------------------------------
sub download_data
{
# location of folder on server computer where file will be uploaded to
$files_location= "/home/ cad 13d";

$query = new CGI;

# get name and path to file on local computer
$ID = $query->param("AvailableFiles");

open(DLFILE, "<$files_location/$ID");
@fileholder = <DLFILE>;
close (DLFILE);

print "Content-Type:application/x-download\n";
print "Content-Disposition:attachment;filename=$ID\n\n";
print @fileholder
}

#----------------------------------------------------------------------------
sub update_txt
# remove downloaded file name from availablefiles.txt
{
# copy available.txt into @file_list array variable
open(AVAILABLE_LIST, "availablefiles.txt");
while(<AVAILABLE_LIST>)
{
push(@file_list, $_);
}
close(AVAILABLE_LIST);

# read through each line of the @file_list
# write each line back to the txt file if
# the line is not the downloaded filename ($ID).
# i.e. remove the downloaded filename from the availablefiles.txt
open(AVAILABLE_LIST, ">./availablefiles.txt");
foreach $line (@file_list)
{
if($line !~ $ID)
{
print AVAILABLE_LIST $line;
}
}
close(AVAILABLE_LIST);

# add downloaded file name to unavailable.txt file
open(UNAVAILABLE_LIST, ">>unavailablefiles.txt");
print UNAVAILABLE_LIST "$ID\n";
close(UNAVAILABLE_LIST);
}

#----------------------------------------------------------------------------
sub print_thankyou
{
print header;
#print "Content-type: text/html\n Window-target: _top\n\n";
print <<END_HTML;

<HTML>
<HEAD>
<TITLE>Thanks!</TITLE>
<META HTTP-EQUIV="Refresh" CONTENT="5; URL=http://notcher.freehostia.com/safesource.cgi">
</HEAD>

<BODY>

<P>Thanks for downloading your file!</P>
You will be redirected to the front page in 5 seconds, or<BR>
you can select this link: <A HREF="
</BODY>
</HTML>

END_HTML
}
#----------------------------------------------------------------------------
 
I don't think you can print two different headers on the same page and have it act normally.

You need to use strict and use warnings (get rid of that -w after perl).. really.. you should also provide full paths to all of your files.

also
this
while(<AVAILABLE_LIST>)
{
push(@file_list, $_);
}
can be changed to
@file_list = <AVAILABLE_LIST>

$line !~ $ID
should be
$line != $ID (And that's assuming they are numeric.. if they aren't change it to ne)


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Yes, forget the thank you page, that can't be done. Once you print the headers to activate the "save as" dialog box you are committed to that and only that.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top