Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

895 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

11 Experts available now in Live!

Get 1:1 Help Now