Solved

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

Posted on 1999-01-04
7
280 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
Comment Utility
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
Comment Utility
Hey! I mean than what must script does after print "content-type...."?
0
 
LVL 7

Expert Comment

by:faster
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Expert Comment

by:sdjjm
Comment Utility
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
Comment Utility
Sorry, I don't need easy way, I need only what I ask.

0
 
LVL 1

Accepted Solution

by:
sdjjm earned 50 total points
Comment Utility
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
Comment Utility
Thank you! It works fine.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

763 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

13 Experts available now in Live!

Get 1:1 Help Now