Mojoli...derFile(3pm)

πŸ“¦
version 0.12 Β· 5 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Mojolicious::Plugin::RenderFile β€” aka Mojoli...derFile(3pm) πŸŽ€

okay bestie, this is a page from the User Contributed Perl Documentation, and it's about Mojolicious::Plugin::RenderFile β€” a whole plugin whose entire personality is handing files to browsers. Let's get into it ✨


🌸 NAME 🌸

Mojolicious::Plugin::RenderFile β€” the "render_file" helper for Mojolicious. that's it, that's the name, and it ate. πŸ’…


✨ SYNOPSIS ✨

like, here's the whole vibe in code, no notes:

# Mojolicious
$self->plugin('RenderFile');

# Mojolicious::Lite
plugin 'RenderFile';

# In controller
$self->render_file('filepath' => '/tmp/files/file.pdf'); # file name will be "file.pdf"

# Provide any file name
$self->render_file('filepath' => '/tmp/files/file.pdf', 'filename' => 'report.pdf');

# Render data from memory as file
$self->render_file('data' => 'some data here', 'filename' => 'report.pdf');

# Open file in browser(do not show save dialog)
$self->render_file(
    'filepath' => '/tmp/files/file.pdf',
    'format'   => 'pdf',                 # will change Content-Type "application/x-download" to "application/pdf"
    'content_disposition' => 'inline',   # will change Content-Disposition from "attachment" to "inline"
    'cleanup'  => 1,                     # delete file after completed
);

see how easy?? iconic behavior honestly 🫢


πŸ¦‹ DESCRIPTION πŸ¦‹

Mojolicious::Plugin::RenderFile is a Mojolicious plugin that adds the "render_file" helper. and here's the part that lowkey matters the most: it does not read the file into memory β€” it just streams it to the client. memory stays unbothered, file goes straight through. main character energy fr fr. ✨


πŸŽ€ HELPERS πŸŽ€

"render_file"

$self->render_file(filepath => '/tmp/files/file.pdf', 'filename' => 'report.pdf' );

with this helper you can give your people files to download, easy peasy. by default the "Content-Type" header is "application/x-download" and the "content_disposition" option value is "attachment". so yeah, a browser is gonna ask where you wanna save the file. πŸ“

if you want the "Content-Type" header to be something else, just pass the "format" option. boom.

Supported Options: (aka the menu, bestie)

  • "filepath" β€” the path on the filesystem to the file. you must always pass either the "filepath" option or the "data" option. no exceptions, it's giving required. πŸ’€
  • "data" β€” binary content which will be transferred to browser. again: you must always pass either "filepath" or "data". pick one, bestie.
  • "filename" (optional) β€” the browser will use this name when saving the file. cute, right?
  • "format" (optional) β€” the "Content-Type" header is based on the MIME type mapping of the "format" option value. and you can extend or change those mappings super easily with "types" in Mojolicious. by default though, the "Content-Type" header is "application/x-download".
  • "content_disposition" (optional) β€” tells the browser how to present the file, and there are exactly two moods:
    • "attachment" (default) β€” is for downloading
    • "inline" β€” is for showing the file inline
  • "cleanup" (optional) β€” indicates if the file should be deleted when rendering is complete. so like, self-cleaning, we love a tidy queen. 🧚

also!! this plugin respects HTTP Range headers. periodt. πŸ”₯


πŸ‘‘ AUTHOR πŸ‘‘

Viktor Turskyi <koorchik@cpan.org>


🌈 CONTRIBUTORS 🌈

Nils Diewald (Akron) Danil Greben (SDSWanderer)


πŸ™ˆ BUGS πŸ™ˆ

found a bug or want a feature? please report any bugs or feature requests to Github: https://github.com/koorchik/Mojolicious-Plugin-RenderFile

(be nice about it, they're doing their best 🫢)


πŸ’œ SEE ALSO πŸ’œ

Mojolicious, Mojolicious::Guides, http://www.mojolicio.us.


Copyright 2011 Viktor Turskyi

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.


okay that's the whole page, you're so welcome πŸ’… this was perl v5.36.0, dated 2022-08-28, and it's the Mojoli...derFile(3pm) page β€” go stream some files, bestie, slay πŸŒΈβœ¨πŸ¦‹

πŸ“¦ where this page comes from