Skip to main content

send_data

Function send_data 

Source
pub fn send_data(
    data: impl Into<Bytes>,
    filename: &str,
    content_type: &str,
    disposition: Disposition,
) -> Response
Expand description

Sends bytes built by the handler as a file (Rails’ send_data): a CSV export, a generated image, an .ics file.

Sets Content-Type, Content-Length and Content-Disposition with filename (sanitized; non-ASCII names kept in filename*). As with serve, Disposition::Inline shows only types that are safe to display (images, PDF, plain text, audio, video) and downloads the rest, and types a browser could run (HTML, SVG, XML, JavaScript) are sent as application/octet-stream. Files already in R2 go through serve instead: it streams them without copying through WebAssembly, and answers Range and If-None-Match (Rails’ send_file). The whole body is in memory, and the Worker’s 128 MB memory limit applies; building it counts toward the 10 ms CPU budget.

§Examples

use ocre::storage::{Disposition, send_data};

let csv = "id,title\n1,Hello\n";
let response = send_data(csv, "posts.csv", "text/csv", Disposition::Download);
assert_eq!(response.headers()["content-type"], "text/csv");
assert_eq!(response.headers()["content-disposition"], "attachment; filename=\"posts.csv\"");