Solved

Outputting files in PERL

Posted on 2002-03-06
8
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

752 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