Solved

Outputting files in PERL

Posted on 2002-03-06
8
194 Views
Last Modified: 2006-11-17
I wish to export a comma delimited flat file database to the user's browser. Which should present them with a "Save As" box but I can't make it work below is what I have.

$newpath = "$basepath" . "AffDBExport.txt";

$|++; ##  Disable output buffering

$nl=$/; ##  Save the newline character
undef $/;
open(F,$newpath) || die $!;
binmode(F); ##  Open the file in binary mode.
$file_contents=<F>; ##  Read the entire file into a single variable
close(F);

$/=$nl; ##  Restore the newline character.

##  Print some junk MIME header
print "Content-type: application/csv\n\n";

print $file_contents;


0
Comment
Question by:noelh
  • 4
  • 2
  • 2
8 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 6846497
noelh,

REPLACE...

print "Content-type: application/csv\n\n";

WITH....

print "Content-type: application/octet-stream\n";
print "Content-disposition: filename='AffDBExport.txt'\n\n"

Let me know how that goes for you.
0
 
LVL 5

Accepted Solution

by:
dkjariwala earned 200 total points
ID: 6847084
#!/usr/bin/perl

$filename = 'AffDBExport.txt';## use single quotes around string in which you don't need to do variable substitution.

$newpath = $basepath . $filename; ##You don't need to put quotes around the variable $basepath.

$|++; ##  Disable output buffering

$nl=$/; ##  Save the newline character
undef $/;
open(F,$newpath) || die $!;


##binmode(F); ##  No need to use binmode.
#no need to use binmode unless you are on Windows and dealing with binary files.
#As in your case, file is ascii, so dont use that code.


$file_contents=<F>; ##  Read the entire file into a single variable
close(F);

print "Content-Type: application/octetstream\n";
print "Content-Disposition: attachment; filename=$filename \n";
print "Pragma: no-cache\n"; ##added this so  that browser doesn't cache it.
print "Expires: 0\n\n";
print $file_contents;


Just check up my comments,
JD
0
 

Author Comment

by:noelh
ID: 6847375
Nope, the browser still just showed the text, instead of offering a "Save As" Choice.

0
 
LVL 5

Expert Comment

by:dkjariwala
ID: 6847570
Which browser you are using ??
JD
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:maneshr
ID: 6847572
noelh,

".. instead of offering a "Save As" Choice...."

Can you post the URL where one can see this script in action?

Meanwhile, i would suggest you REPLACE...

print "Content-type: application/octet-stream\n";

WITH...

print "Content-type: application/noelh\n";

& let me know how it goes.
0
 
LVL 5

Expert Comment

by:dkjariwala
ID: 6847589
Yeah, Agree with maneshr,

Put up the URL so that we can check in our browser.

JD


0
 

Author Comment

by:noelh
ID: 6847773
Sorry, upon double checking I noticed that I had not transfered your code correctly. Once I fixed my mistake it worked great. Thanks!!
0
 
LVL 5

Expert Comment

by:dkjariwala
ID: 6849659
Great !!

JD
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now