Solved

Outputting files in PERL

Posted on 2002-03-06
8
183 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
Comment Utility
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
Comment Utility
#!/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
Comment Utility
Nope, the browser still just showed the text, instead of offering a "Save As" Choice.

0
 
LVL 5

Expert Comment

by:dkjariwala
Comment Utility
Which browser you are using ??
JD
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 16

Expert Comment

by:maneshr
Comment Utility
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
Comment Utility
Yeah, Agree with maneshr,

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

JD


0
 

Author Comment

by:noelh
Comment Utility
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
Comment Utility
Great !!

JD
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

12 Experts available now in Live!

Get 1:1 Help Now