Solved

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

Posted on 2006-06-13
2
303 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
text on slider doesn't resize nicely 5 58
jquery to restrict certain words from input in form 11 35
REReplaceNoCase help 1 33
Fulfillment API php code sample 1 41
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

809 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