Solved

Return "content-type: application/zip" as result.

Posted on 1999-01-04
7
283 Views
Last Modified: 2013-12-25
I create CGI/perl script for downloading resources from my site. After user posts some info (name, e-mail) the script must return "content-type: application/zip" with some predefined file as result.
How can I return this?

P.S. it's runnings under Apache v1.3.2/linix & perl5
0
Comment
Question by:plr
7 Comments
 
LVL 11

Expert Comment

by:mouatts
ID: 1830383
The first thing that your CGI must output is an HTTP header.

The actual header that you want will look as follows

print "Content-type: application/x-zip\n\n"

The two /n (newlines) indicate that it is the end of the HTTP header. However I know that at least oone perl CGI library provides its own routines for outputing the mime types and if you are using one of these then you should use these as functions.

HTH
Steve
0
 

Author Comment

by:plr
ID: 1830384
Hey! I mean than what must script does after print "content-type...."?
0
 
LVL 7

Expert Comment

by:faster
ID: 1830385
After the http response header comes the response body, for your case, it is probably a zipped file.  Which means that you should read a file in binary mode and then write its contents to stdout.  You can use read() and write() to do this.  It is better to first get the size of the file and output it in the content-length header.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Expert Comment

by:sdjjm
ID: 1830386
Why not make it easy on yourself and use the following instead of the "Content-type yada-yada" header and the hassle of reading the binary file? Just assign $filename to what they select in your form.  Works well for me...

#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
$filename="whatever.zip";
print "Click <A HREF=\"$filename\"><B>HERE</B></A> to start your download";
exit;

0
 

Author Comment

by:plr
ID: 1830387
Sorry, I don't need easy way, I need only what I ask.

0
 
LVL 1

Accepted Solution

by:
sdjjm earned 50 total points
ID: 1830388
OK then,  here's a script that will do exactly what you want and send a file via the "Content-type" header.  You DO need the "Content-Disposition" and "Content-Length" before it to tell the browser the file name and size you are sending.  Just change $name to the correct file name you want to send.

#!/usr/local/bin/perl
$name = "whatever.zip";        #put file name here or assign $name from user input
open(DAT, $name)||&error;
binmode(DAT);
$size= -s $name;
print "Content-Disposition: filename=\"$name\"\n";
print "Content-Length: $size\n";
print "Content-type: application/x-zip\n\n";
while (read(DAT, $line, 76)) {
print "$line";
}
close DAT;
exit;
sub error {
   print "Content-type: text/html\n\n";
   print "Error Opening File - $name";
   exit;
};

0
 

Author Comment

by:plr
ID: 1830389
Thank you! It works fine.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

856 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