Perl CGI 通过网络浏览器下载文件 [英] Perl CGI to download a file via web browser

查看:43
本文介绍了Perl CGI 通过网络浏览器下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一些可以从服务器机器下载文件的 cgi-perl 脚本.EX:点击下载链接,它会打开一个另存为"窗口并允许我将文件保存在本地计算机上.

Need some cgi-perl script which can download a file from a server machine. EX: click on a download link and it will open a "save as" window and will allow me to save the file on my local machine.

我使用 CGI 创建了一个网页,使用它我将一个文件上传到服务器,并将运行一个 perl 脚本将其转换为其他格式(到这里我已经完成了).现在我需要把这个文件取回(下载回来)到系统中.

I have created a web page using CGI, using this I will upload a file to server and will run a perl script to convert it to some other format (till here I am done). Now I need get this file back (download back) to the system.

#!/usr/bin/perl
#print "Content-type: text/html

";
my $filepath='/upload/testing.pm';

    print "Content-Type: text/html

";

    open("DOWNLOADFILE", "<".$filePath);
    while($fileContents = <DOWNLOADFILE>) {
        print $fileContents;
    }
print "Content-Type: text
";
print "Content-Disposition: attachment; filename='testing.pm'
";
print "Content-Description: File to download

";
    close DOWNLOADFILE;

将文件从我的机器(客户端)上传到服务器机器,在那里我有一个 perl 脚本,它会将文件转换为另一种格式,并将新转换的文件保存到目录中,例如:/upload-> 直到我在这里用脚本完成.

Upload a file from my machine (client) to server machine, where I have a perl script which will convert the file to another format and will save the newly converted file to a dir ex: /upload-> till here I am done with scripting.

现在我需要使用浏览器将此文件下载回客户端计算机.在这种情况下,我试图将 testing.pm 文件下载回客户端机器.

Now I need to download this file back to client machine using browser. In this case I was trying to download testing.pm file back to client machine.

推荐答案

重新排列 HTML 标题有效.这是工作脚本.可以原样使用它

Rearranging the HTML headers worked. here is the working script. one can use this as it is

use CGI;
my $html= new CGI;
#get the file name from URL ex. http://<server_IP>/cgi-bin/download.cgi?a=testing.txt
my $file= $html->param('a'); 
# $file is nothing but testing.txt
my $filepath= "/var/www/upload/$file";

print ("Content-Type:application/x-download
");
print "Content-Disposition: attachment; filename=$file

";

open FILE, "< $filepath" or die "can't open : $!";
binmode FILE;
local $/ = 10240;
while (<FILE>){
    print $_;
}

    close FILE;
 # use Unlink if u want to delete the files after download.
#unlink ($filepath);

这篇关于Perl CGI 通过网络浏览器下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆