I would like to send application/x-download header

642 views
Skip to first unread message

メメメ

unread,
Jun 1, 2010, 7:47:55 AM6/1/10
to Mojolicious
Hi.

I would like to send response which contains application/x-download
header.
How can I do?


My code is:

#!/usr/bin/env
perl
use Mojolicious::Lite;
use FindBin;

get '/download_zip' => sub {
my $self = shift;

my $zip_path = $FindBin::Bin . "/../data/test.zip";
my $data = '';
open my $fh, '<', $zip_path or die "Can't open \"$zip_path\"$!";
while(sysread($fh, my $buf, 1024)) {
$data .= $buf;
}
close($fh);

# I would like to send
response:
# "Content-type: application/x-download; name=test.zip
\n";
# "Content-Disposition: attachment; filename=test.zip
\n";
# "Content-Description: zip\n
\n";
# $data;

# This code does not
work.
$self->res->headers->content_type('application/zip');
$self->res-
>body($data);
};


app->start;

vti

unread,
Jun 1, 2010, 7:52:01 AM6/1/10
to mojol...@googlegroups.com
And what does it do instead?

And maybe you should also use chunked encoding for sending big files, so mojo will not hold it in memory.

Also you can use ->serve_static for serving static files.

> --
> You received this message because you are subscribed to the Google Groups "Mojolicious" group.
> To post to this group, send email to mojol...@googlegroups.com.
> To unsubscribe from this group, send email to mojolicious...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mojolicious?hl=en.
>

vti

unread,
Jun 1, 2010, 9:09:09 AM6/1/10
to mojol...@googlegroups.com
Sebastian Riedel suggests using:

$res->content->asset(Mojo::Asset::File->new(path => 'lalala/foo.zip'));

On Jun 1, 2010, at 1:47 PM, メメメ wrote:

メメメ

unread,
Jun 1, 2010, 9:52:34 AM6/1/10
to Mojolicious
Thanks.
I used both render_static() and $res->content->asset();

Finally, My code is:
#!/usr/bin/env
perl
use Mojolicious::Lite;
use FindBin;
use Data::Dumper;

get '/download_zip' => sub {
my $self = shift;

my $zip_path = $FindBin::Bin . "/../data/test.zip";
my $data = '';
open my $fh, '<', $zip_path or die "Can't open \"$zip_path\"$!";
while(sysread($fh, my $buf, 1024)) {
$data .= $buf;
}
close($fh);


$self->res->content->asset(Mojo::Asset::File->new(path =>
$zip_path));

my $headers = Mojo::Headers->new;
$headers->add('Content-Type','application/x-download;
name=test.zip');
$headers->add('Content-Disposition','attachment;
filename=test.zip');
$headers->add('Content-Description','zip');
$self->res->content->headers($headers);

$self->render();


# using
render_static()
#$self-
>render_static('test.zip');
};


app->start;

Charlie Brady

unread,
Dec 10, 2010, 5:55:40 PM12/10/10
to mojol...@googlegroups.com

On Tue, 1 Jun 2010, vti wrote:

> And what does it do instead?
>
> And maybe you should also use chunked encoding for sending big files, so
> mojo will not hold it in memory.

How do I do that? I think that I need to make a callback which provides a
chunk at a time, but I'm not certain how to attach that to the route
handler in Mojolicious::Lite;

I think this from Mojo/HelloWorld.pm is likely relevant:


sub _chunked_params {
my ($self, $tx) = @_;

# Chunked
$tx->res->headers->transfer_encoding('chunked');

# Chunks
my $params = $tx->req->params->to_hash;
my $chunks = [];
for my $key (sort keys %$params) {
push @$chunks, $params->{$key};
}

# Callback
my $cb;
$cb = sub {
my $self = shift;
my $chunk = shift @$chunks || '';
$self->write_chunk($chunk, $chunk ? $cb : undef);
};
$cb->($tx->res);
$tx->resume;

Reply all
Reply to author
Forward
0 new messages