NAME

PAGI::FastAPI::Response::File - File Download Response for PAGI::FastAPI

VERSION

Version v1.2.5

SYNOPSIS

use PAGI::FastAPI::Response::File qw(file_response);

$app->get('/reports/{id}',
    handler => async sub ($c) {
        my $path = "/var/reports/" . $c->path_param('id') . ".pdf";
        return file_response($path);
    }
);

# Inline (view in browser) instead of "Save As" download prompt:
$app->get('/logo.png',
    handler => async sub ($c) {
        return file_response('/var/assets/logo.png', as_attachment => 0);
    }
);

# Or construct directly:
use PAGI::FastAPI::Response::File;

my $res = PAGI::FastAPI::Response::File->new(
    body     => $binary_data,
    headers  => [ ['content-type', 'application/pdf'] ],
    filename => 'report.pdf',
);

FUNCTIONS

file_response($path, %opts)

Exported on request. Reads $path from disk and returns a ready-to-return PAGI::FastAPI::Response::File instance.

  • content_type - (Optional) Overrides the guessed MIME type. Recognises .txt, .html, .json, .csv, .pdf, .png, .jpg/.jpeg, .gif, .svg by extension; falls back to application/octet-stream.

  • filename - (Optional) Overrides the Content-Disposition filename. Defaults to the basename of $path.

  • as_attachment - (Optional) Boolean, defaults to true. When true, sends Content-Disposition: attachment; filename="...", prompting a download. Set to false for inline display (e.g. images shown directly in the browser).

  • status - (Optional) HTTP status code. Defaults to 200.

Dies if $path doesn't exist or isn't readable.

METHODS

new(%options)

  • body - (Required, inherited) The raw file content.

  • headers - (Optional, inherited) ArrayRef of [name, value] pairs; set content-type here.

  • filename - (Optional) If set, adds a Content-Disposition: attachment; filename="..."-header automatically.

  • status - (Optional, inherited) Defaults to 200.

SEE ALSO

PAGI::FastAPI::Response, PAGI::FastAPI::Response::HTML, PAGI::FastAPI::Response::SSE

AUTHOR

Mohammad Sajid Anwar, <mohammad.anwar at yahoo.com>

REPOSITORY

https://github.com/manwar/PAGI-FastAPI

BUGS

Please report any bugs or feature requests through the web interface at https://github.com/manwar/PAGI-FastAPI/issues. I will be notified and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc PAGI::FastAPI::Response::File

You can also look for information at:

LICENSE AND COPYRIGHT

Copyright (C) 2026 Mohammad Sajid Anwar.

This program is free software; you can redistribute it and/or modify it under the terms of the Artistic License (2.0).