Solved

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

Posted on 1999-01-04
7
284 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

730 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