File export TypeScript helper for React
v1.0.0Added in 1.0.0
File export helpers for React apps: download blobs, text and JSON, turn a canvas or an SVG into a PNG, copy images and name files cleanly.
The card is an SVG. Download SVG saves it as it is, Download PNG rasterizes it at the playground scale (with the background color when one is set), Copy PNG puts the image on the clipboard (paste it into a chat or a doc), and Download JSON saves the card's data. Every file is named from the title plus the local date and time, and the readout shows the last name and size.
File export is a Pro React helper in the React hooks and utilities category, written in TypeScript for React 19. It installs as source with npx wingo-ui@latest add file-export.
Add to a project
Requirements: React 19, in Next.js (App Router or Pages), Vite or React Router. Installation guide · Next.js setup · Vite setup
npx wingo-ui@latest add file-exportFile export is part of Pro, with 298 more Pro items: $8 a month or $150 once. 14-day refund before you download any Pro item. See plans
Usage
Import downloadBlob from @/lib/file-export.
import { svgToImageBlob, downloadBlob, timestampedFileName } from "@/lib/file-export"
export function DownloadQr({ svg }: { svg: SVGSVGElement | null }) {
return (
<button
type="button"
onClick={async () => {
if (!svg) return
const png = await svgToImageBlob(svg, { scale: 4, background: "#ffffff" })
downloadBlob(png, timestampedFileName("invoice-qr-code", "png"))
}}
>
Download PNG
</button>
)
}API
scaledefault2numbersvgToImageBlob(svg, { scale }): device pixels per CSS pixel of the PNG. 2 keeps exports sharp on retina screens and in print.
backgrounddefault""stringsvgToImageBlob(svg, { background }): a fill behind the image; empty keeps the PNG transparent.
fileNamedefault"Certificate Emma Carter"stringThe base passed to timestampedFileName(base, extension): diacritics and unsafe characters are removed, the local date and time are appended.
downloadBlob(blob: Blob, filename: string) => voidThe main helper: saves a Blob under a file name through a temporary object URL, released right after. Browser only; call it from an event handler, never during render.
Parts
downloadUrl
functionSaves a URL (same origin, or a data: or blob: URL) under a file name through a temporary link. Browser only, for event handlers.
downloadText
functionSaves text such as a log, a diff or a CSV under a file name. Options: type (default text/plain) and bom, which prefixes a UTF-8 BOM so Excel on Windows reads accented letters.
downloadJson
functionSaves any value as pretty-printed JSON (2 spaces by default) under a file name.
canvasToBlob
functioncanvas.toBlob as a promise for PNG, JPEG or WebP. Rejects when a cross-origin image has tainted the canvas.
serializeSvg
functionStandalone SVG text from an element or a string, with the namespaces and XML header added so the file opens in any viewer.
svgToBlob
functionThe same standalone SVG as an image/svg+xml Blob, ready for downloadBlob.
svgToImageBlob
functionDraws an SVG into a canvas and returns a PNG, JPEG or WebP Blob: QR codes, signatures, charts. Options: width, height, scale (default 2), background, type, quality. The size defaults to the SVG's own width, height or viewBox.
loadImage
functionAn image ready for drawImage from a URL, a data URL or a File or Blob; an object URL is released once the image decodes. Croppers and avatar uploads use it.
blobToDataUrl
functionA data: URL from a Blob, for previews and small uploads.
canCopyImages
functionWhether this browser can put an image on the clipboard (ClipboardItem and clipboard.write exist). Check it in an effect or a handler and hide the Copy button when it is false.
copyImageToClipboard
functionPuts a PNG on the clipboard. Pass the promise itself from the click handler, not an awaited blob: Safari only allows the write when the clipboard item is created inside the gesture. Resolves false when unsupported or denied.
sanitizeFileName
functionStrips what file systems refuse and keeps names readable: "Invoice #12/2026" becomes "Invoice-12-2026", "Café menu" becomes "Cafe-menu". Falls back to "file" when nothing is left.
timestampedFileName
functionA clean base name plus the local date and time, so repeated exports never collide: ("Signature", "png") gives "Signature-2026-09-24-1640.png".
ImageType
type"image/png" | "image/jpeg" | "image/webp", the raster formats the helpers produce.
RasterizeOptions
typesvgToImageBlob's options: width, height, scale, background, type, quality.
Examples
Clean file names
sanitizeFileName strips what file systems refuse; the timestamp keeps exports from colliding.
Export a report
downloadText with a BOM for Excel, and downloadJson for the same rows.
Copy a chart
copyImageToClipboard with the rasterize promise passed straight from the click.
Dependencies
No dependencies beyond React.
Files
- lib/file-export.ts
FAQ
Is the File export helper free?
The preview, props and docs are free; copying the source needs a Pro plan. See plans.
How do I install File export?
Run npx wingo-ui@latest add file-export. With shadcn, add the @wingo-ui registry with your API key, then run npx shadcn@latest add @wingo-ui/file-export.
See also
Used in
More React hooks and utilities
Articles using this component
- React QR Code with Logo: Rounded Modules, PNG and SVG
Component guides9 min read
- React Signature Pad: Smooth Ink, Typed Mode, PNG and SVG
Component guides9 min read