Menu

Guides

Downloads

Decide where the files a page downloads are saved.

A page downloads a file when a server answers a link with an attachment, a link has a download attribute or the page exports a blob: or data: URL.

What happens by default

Kurogane asks the user where to save each file with the system’s Save As dialog. The dialog fills in the suggested name. When the user cancels nothing is saved. No page writes to the disk unless the user picked the place.

A window shows one dialog at a time. A page that downloads several files asks about each in turn. Chromium’s own behavior of saving silently into the Downloads folder is never used.

Save your app’s exports without asking

App::on_download decides per download. Save your own pages’ exports into a folder of your app’s and refuse everything else:

use kurogane::{App, DownloadDecision, Origin};

fn main() {
    let exports = std::env::temp_dir().join("my-app-exports");
    let own = Origin::parse("app://app").unwrap();

    App::new("content")
        .on_download(move |download, _app| {
            if download.origin() == &own {
                DownloadDecision::SaveTo(exports.join(download.suggested_name()))
            } else {
                DownloadDecision::Deny
            }
        })
        .run_or_exit();
}
DownloadDecision What happens
SaveTo(path) saves at that absolute path without asking and replaces a file already there
Prompt asks the user with the Save As dialog
Deny saves nothing
Default Kurogane’s policy, which asks the user

SaveTo creates the folder when it’s missing. Nothing is saved when that fails or the path is relative.

Know where a download comes from

DownloadRequest describes the download:

  • origin() is the origin of the page the download comes from, not of the file. A page of your own may export a blob: or link to a file anywhere.
  • url() is the URL the file comes from.
  • mime_type() is the file’s MIME type. It’s empty when unknown.
  • suggested_name() is a file name only. Chromium has already removed any folder a page tried to put in it. Joining it onto a folder of your own stays in that folder.
  • browser() names the browser the download comes from.

Download one of your own files

A download link to one of your app’s own files (<a href="report.pdf" download>) does nothing. Chromium doesn’t download from app:// that way. Fetch the file and download it as a blob:. That reaches the hook like any other download:

const response = await fetch("report.pdf");
const link = document.createElement("a");
link.href = URL.createObjectURL(await response.blob());
link.download = "report.pdf";
link.click();

Save from Rust instead

A command can write the file itself. Grants and download rules bind pages only. Send the data from the page and write it where your app decides:

use kurogane::{App, AppHandle, IpcError};

fn main() {
    App::new("content")
        .binary_command("export", |data: &[u8], _: &AppHandle| {
            let path = std::env::temp_dir().join("export.bin");
            std::fs::write(&path, data).map_err(|e| IpcError::from(e.to_string()))?;
            Ok(Vec::new())
        })
        .run_or_exit();
}
await window.kurogane.invoke("export", new TextEncoder().encode("report"));

Several files at once

A page may download several files without a click for each. Every one of them still reaches on_download or the Save As dialog. Chromium’s own prompt for multiple downloads never shows.

Rules for the hook

  • It runs on the UI thread before the download starts and must not block.
  • A hook that panics refuses the download.
  • Downloads from DevTools never reach it and always ask the user.
  • A later call replaces an earlier hook.