3

我开始查看 PSGI,我知道应用程序的响应应该是三个元素的数组 ref,[code, headers, body]:

#!/usr/bin/perl

my $app = sub {
  my $env = shift;

  return [
    200,
    [ 'Content-type', 'text/plain' ],
    [ 'Hello world' ],
  ]
};

问题是如何将文件(例如 zip 或 pdf)发送到浏览器以供下载。

4

2 回答 2

8

只需设置正确的标题和正文。

my $app = sub {
  my $env = shift;

  open my $zip_fh, '<', '/path/to/zip/file' or die $!;

  return [
    200,
    [ 'Content-type', 'application/zip' ], # Correct content-type
    $zip_fh, # Body can be a filehandle
  ]
};

您可能想尝试添加其他标题(特别是“Content-Disposition”)。

于 2014-07-26T08:08:14.017 回答
2

看看perl dancer;它具有 psgi 支持,是一个非常轻量级的框架。

例子:

 #!/usr/bin/env perl
 use Dancer;

 get '/' => sub {
     return send_file('/home/someone/foo.zip', system_path => 1);
 };

 dance;

运行 chmod 0755 ./path/to/file.pl; ./path/to/file.pl

致电:

wget <host>:<port>/

于 2014-07-26T00:55:39.567 回答