Solved

THIS CGI CODE DOES NOT output as csv file.PLS HELP

Posted on 2006-06-13
2
300 Views
Last Modified: 2012-08-14
hi Experts

i need some help with this code

this code actually generates some data which should be dowloaded as csv file.. but the problem with this code is.. it dowloads as evpData.cgi instead of evpdatA.CSV

CAN ANYONE PLEASE MODIFY THE CODE SO THT IT IS DOWNLOADED AS CSV FILE

ITS URGENT .. PLS HELP..
---------------------------------------------------------------------------
#!/bin/env perl

#print "Content-type: application/vnd.ms-excel\n\n";
#print "Content-type: application/csv.ms-excel\n\n";
#print "Content-type: application/comma-separated-values\n\n";
#print "Content-type: text/comma-separated-values\n\n";
print "Content-type: text/csv\n\n";

require "../cgi-lib.pl";
require "../ppdInclude.pl";
&ReadParse;

my $xLat = sprintf("%s%s", $in{'decDegreesLong'},
$in{'decMinutesLong'});
my $yLong= sprintf("%s%s", $in{'decDegreesLat'}, $in{'decMinutesLat'});

#if ($x < 112 || $x > 154) {
#        print "longitude values are 112..154 inclusive\n\nYour value is
$x\n";
#        exit();
#}
#
#if ($y < -44 || $y > -10) {
#        print "latitude values are -10..-44 inclusive\n\nYour value is
$y\n";
#        exit();
#}

# make the latitude and longitude are in 0.05 degree granularity
my $yy= int(($yLong-0.025)*20);
$y=-$yy/20.0;

$x= int(($xLat-112+0.025)*20);

my $datafile;
$datafile="data/output_dir_by_lat/$yLong";

my @bits;
open(INPUT, $datafile) or die "Can't find datafile $datafile: $!\n";

        print ",Monthly\n";
        print ",Evaporation\n";
        print ",(mm)\n";
print ",\n";
$_=<INPUT>; @bits = split(' ', $_, 9999); print "January,",
"$bits[$x]\n";
$_=<INPUT>; @bits = split(' ', $_, 9999); print "February,",
"$bits[$x]\n";
$_=<INPUT>; @bits = split(' ', $_, 9999); print "March,",
"$bits[$x]\n";
$_=<INPUT>; @bits = split(' ', $_, 9999); print "April,",
"$bits[$x]\n";
$_=<INPUT>; @bits = split(' ', $_, 9999); print "May,",
"$bits[$x]\n";
$_=<INPUT>; @bits = split(' ', $_, 9999); print "June,",
"$bits[$x]\n";
$_=<INPUT>; @bits = split(' ', $_, 9999); print "July,",
"$bits[$x]\n";
$_=<INPUT>; @bits = split(' ', $_, 9999); print "August,",
"$bits[$x]\n";
$_=<INPUT>; @bits = split(' ', $_, 9999); print "September,",
"$bits[$x]\n";
$_=<INPUT>; @bits = split(' ', $_, 9999); print "October,",
"$bits[$x]\n";
$_=<INPUT>; @bits = split(' ', $_, 9999); print "November,",
"$bits[$x]\n";
$_=<INPUT>; @bits = split(' ', $_, 9999); print "December,",
"$bits[$x]\n";

while (<INPUT>) {
        my @bits = split(' ', $_, 9999);
        print "$bits[$x]\n";
}
------------------------------------------------------
0
Comment
Question by:GouthamRDodda
2 Comments
 
LVL 16

Expert Comment

by:OliWarner
ID: 16899914
Setting the content type will not get around the filename, only which application the browser thinks to open it with.

To do that you need to look into URL rewriting. If you're using apache, this should be really easy... If you dont know what it is, URL Rewriting is having a fake URL that maps onto a real file... In your case: whatever.csv maps onto your .cgi file.

All you need to do (with a bit of editing of course) is put the following in a file called .htaccess in your root directory:
RewriteEngine on
RewriteRule   ^/evpData.csv$  evpData.cgi  [R]

So when you request evpData.csv from the server, you get the output of the CGI file. The browser wont know that its being fed from the cgi file and therefore will attempt to name it a .csv
0
 
LVL 6

Accepted Solution

by:
nitinsawhney earned 500 total points
ID: 16900222
Hi,

Try changing your headers to following:

        print "Content-type: text/html\n";
        print "Content-Disposition: attachment; filename=$name.csv\n\n";

Here $name could be anything for e.g. evpData

Hope this helps.

Cheers!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Read about why website design really matters in today's demanding market.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

759 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

20 Experts available now in Live!

Get 1:1 Help Now