Solved

Outputting files in PERL

Posted on 2002-03-06
8
210 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 5

Expert Comment

by:dkjariwala
ID: 6847570
Which browser you are using ??
JD
0
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

821 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