Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 1999-01-04
7
Medium Priority
?
289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

 
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 200 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

718 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