{"success":true,"name":"file-download-endpoints-set-the-headers-name-the-file-and-st","description":"Serving a file for download and the browser mangles, misnames, or inlines it","category":"ai-brimind","path":"ai-brimind/file-download-endpoints-set-the-headers-name-the-file-and-st/SKILL.md","file":"SKILL.md","content":"---\nname: file-download-endpoints-set-the-headers-name-the-file-and-st\ndescription: \"Serving a file for download and the browser mangles, misnames, or inlines it\"\n---\n\n# File Download Endpoints: Set the Headers, Name the File, and Stream — Don't Improvise\n\n## When to use\nYou are building an endpoint that returns files (exports, invoices, uploads), and download behavior, filenames, or large-file handling is left to defaults.\n\n## Example request\nPDF opens as garbage text in the tab → wrong or missing content type; mislabeled payload. every download saves as 'download' or the route name → no disposition header named it; anonymous file. an accented filename arrives butchered → non-ASCII names need the encoded header form; mangled name. 500MB export buffered fully in memory → per the streaming rules, a self-inflicted outage; gulped file. — each shipped bytes and forgot the envelope.\n\n## Instructions\nAct as a shipping clerk who labels every parcel with what it is, what to call it, and how to handle it — because an unlabeled parcel arrives as a mystery. A file response is bytes plus three decisions, all yours: the content type header telling the browser what the bytes are (the real MIME type — a wrong one turns PDFs into gibberish tabs and triggers security sniffing); the disposition header deciding inline display versus download-and-save, and carrying the filename — build that filename deliberately (meaningful, dated, extension matching the type) and use the standard encoded form for anything beyond plain ASCII, because accents in the naive form arrive mangled on some browsers; and the delivery method — stream from storage to response per the chunking rules, never read-whole-then-send, and include the size header when known so users get real progress bars. Guard the door too: downloads of private files pass the same permission checks as any route, per the private-files rules — an unguarded download URL is a leak with a filename. Generate-then-download flows (big exports) follow the slow-work rules: prepare in the background, deliver when ready. Test the unglamorous way: download with an accented filename, a huge file, and the wrong user, on more than one browser. Only proceed once every file leaves correctly labeled, correctly named, permission-checked, and streamed.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #816\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}