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

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

问题描述

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

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\n\n";
my $filepath='/upload/testing.pm';

    print "Content-Type: text/html\n\n";

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

将文件从我的机器(客户端)上传到服务器机器,在这里我有一个perl脚本,该脚本会将文件转换为另一种格式,并将新转换的文件保存到dir ex:/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\n");
print "Content-Disposition: attachment; filename=$file\n\n";

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天全站免登陆