Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello everyone I really tried to fix this problem but this time I need his help. This code work to download a file with little size:
use strict; use warnings; use Plack::Response; use Path::Tiny; use File::MimeInfo; sub return_psgi { my $self = shift; my $path_file = shift; my $content_type = mimetype($path_file); $content_type = 'application/octet-stream' if not defined $content +_type; my $content = path($path_file); my $content_download = $content->slurp; require File::Spec::Unix; my $basename = $path_file; $basename =~ s|\\|/|g; $basename = ( File::Spec::Unix->splitpath($basename) )[2]; $basename =~ s|[^\w\.-]+|_|g; my $response = Plack::Response->new(200); $response->headers( [ 'Content-Type' => $content_type, 'Content-Disposition' => qq[attachment; filename="$basename +"], ], ); $response->body($content_download); return $response->finalize; }
But I want to download big files greater of 1GB, so change to download in chunks, to this code:
open (FILE, "< $path_file") or die "can't open $path_file: $!"; binmode FILE; local $/ = \102400; while (<FILE>) { my $var = scalar $_; $response->body($var); return $response->finalize; } close FILE;
Another intent, using autoflush:
use IO::Handle; my $BLOCK_SIZE = 1024 * 1024; open(my $fh, "<:raw", $path_file) or die "Can't read from input file $ +path_file $!\n"; while (read($fh, $buffer, $BLOCK_SIZE)) { $response->body($buffer); $fh->autoflush; } close $fh; return $response->finalize;
The 2 previous intentions download file a size of kb, corrupt file.

can you please tell me where is my error in my code?

In reply to Download big file in chunks with plack response by Thenothing

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-19 19:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found