Solved

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

Posted on 1999-01-04
7
281 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
DevOps Toolchain Recommendations

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

 
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

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

Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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…

867 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

16 Experts available now in Live!

Get 1:1 Help Now