Link to home
Start Free TrialLog in
Avatar of steviewonder
steviewonder

asked on

CGI script to download a file from server to client's directory

Hi,

The CGI script will download a specified file from webserver to client's pc, and save it to a predefined directory on the client.
Of course, it will prompt user to hit OK to save, but no filename and directory needed to be entered. (as filename and directory are specified in CGI script)

thanks.
ASKER CERTIFIED SOLUTION
Avatar of lexxwern
lexxwern
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of steviewonder
steviewonder

ASKER

If I can not do so with CGI, what is my alternate choice?

thanks.
i just cannot be done,

it becomes a very serious security issue if it can be done by any means...

sorry.
steviewonder,

You can use cgi scripts which lets users to download some applications/files. You can try this:

--------------- download.cgi ---------------------------
#!/usr/bin/perl

use CGI;
use strict;

my $q = new CGI;
my $path = '/path/to/files/';
my $fname = $q->param('filename');
my $filepath = "$path/$fname";
my $size= -s $filepath;
my $buff;

print "Content-type: application/octet-stream\r\n";
print "Content-Length: $size\r\n";
print "Content-disposition: attachment;filename=$fname\r\n\r\n";
open(FILE, "<$filepath") || die;
binmode FILE;
binmode STDOUT;
while (read FILE, $buff, 1024) {
  print $buff;
}
close(FILE);

------------------------------------------------------

The above script will allow you to download any file passed as parameter to it.

you can access the script as:
http://www.somedomain.com/cgi-bin/download.cgi?filename=xyz.zip

Hope this is what you are looking at...

Aman.


amandeep,
I'd tried C program below (translated from your Perl code), but did not work. Is there something wrong w/ the way I called cgi file (download.exe)

piece of my C code:

printf( "Content-type: application/octet-stream\r\n");
printf( "Content-Length: 39518\r\n");
printf("%s",fname);

printf( "Content-disposition: attachment;filename=mydownload.txt\r\n\r\n");

/*     if( (download_file = fopen( fname, "r" )) == NULL )
     printf( "The file was not opened\n" );

     post = new char[max_line_len];
     
     while (!feof(download_file))
     {
          fgets(post,max_line_len,download_file);
          printf(post);
     }          
     
     fclose(download_file);
*/

THIS IS THE HTML PAGE's CODE:

<FORM ENCTYPE="multipart/form-data" ACTION="http://test/cgi-bin/download.exe" METHOD=GET>
Download the file from server: <INPUT TYPE="submit" VALUE="Download File">
</FORM>
amandeep,
I'd tried C program below (translated from your Perl code), but did not work. Is there something wrong w/ the way I called cgi file (download.exe)

piece of my C code:

printf( "Content-type: application/octet-stream\r\n");
printf( "Content-Length: 39518\r\n");
printf("%s",fname);

printf( "Content-disposition: attachment;filename=mydownload.txt\r\n\r\n");

/*     if( (download_file = fopen( fname, "r" )) == NULL )
     printf( "The file was not opened\n" );

     post = new char[max_line_len];
     
     while (!feof(download_file))
     {
          fgets(post,max_line_len,download_file);
          printf(post);
     }          
     
     fclose(download_file);
*/

THIS IS THE HTML PAGE's CODE:

<FORM ENCTYPE="multipart/form-data" ACTION="http://test/cgi-bin/download.exe" METHOD=GET>
Download the file from server: <INPUT TYPE="submit" VALUE="Download File">
</FORM>
amandeep,
I'd tried C program below (translated from your Perl code), but did not work. Is there something wrong w/ the way I called cgi file (download.exe)

piece of my C code:

printf( "Content-type: application/octet-stream\r\n");
printf( "Content-Length: 39518\r\n");
printf("%s",fname);

printf( "Content-disposition: attachment;filename=mydownload.txt\r\n\r\n");

/*     if( (download_file = fopen( fname, "r" )) == NULL )
     printf( "The file was not opened\n" );

     post = new char[max_line_len];
     
     while (!feof(download_file))
     {
          fgets(post,max_line_len,download_file);
          printf(post);
     }          
     
     fclose(download_file);
*/

THIS IS THE HTML PAGE's CODE:

<FORM ENCTYPE="multipart/form-data" ACTION="http://test/cgi-bin/download.exe" METHOD=GET>
Download the file from server: <INPUT TYPE="submit" VALUE="Download File">
</FORM>
I have noticed that you have 9 opened questions that are quite old. You have a responsibility to close these questions out.
You can get to them by clicking on your name link at the upper left of the web page.

https://www.experts-exchange.com/questions/20181671/usr-xpg4-bin-id-command-results-different-on-solaris-8-and-2-6.html
https://www.experts-exchange.com/questions/20181063/configuring-multi-home-solaris-8-box.html
https://www.experts-exchange.com/questions/20357606/java-applet-error-running-our-Internet-Explorer-5-5.html
https://www.experts-exchange.com/questions/20375901/CGI-to-display-result-on-a-new-broser-window-in-IE.html
https://www.experts-exchange.com/questions/20361952/upload-file-using-CGI.html
https://www.experts-exchange.com/questions/20336471/CGI-script-to-download-a-file-from-server-to-client's-directory.html
https://www.experts-exchange.com/questions/20164644/Using-Internet-Connection-Sharing-on-Win98-with-a-WinNT-PC.html
https://www.experts-exchange.com/questions/20338617/get-error-failure-audit-when-creating-custom-receipient-in-exchange.html
https://www.experts-exchange.com/questions/20164835/Import-Exchange-5-5-data-into-Exchange-2000-in-a-different-box.html


Please close all of your old questions (older than a month) within 7 days or I have a duty to report this to the site administrators who may take action on your account.
If you have question on how to dispose of a question that doesn't have an answer, just ask me by dropping a line on this question and I will assist you.
Thanks for helping keep Experts Exchange clean

DigitalXtreme
CS Moderator
Force Accepted to lexxwern

SpideyMod
Community Support Moderator @Experts Exchange