Skip to content

web: download data to file #3571

Answered by wmte
wmte asked this question in Q&A
Nov 18, 2023 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

I have created a fairly awful solution;

		use web_sys;
		use wasm_bindgen::JsCast;

		let win = web_sys::window().unwrap();
		let doc = win.document().unwrap();

		let link = doc.create_element("a").unwrap();
		link.set_attribute("href", "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR42mP4z8AAAAMBAQD3A0FDAAAAAElFTkSuQmCC");
		link.set_attribute("download", "output.png");

		let link: web_sys::HtmlAnchorElement = web_sys::HtmlAnchorElement::unchecked_from_js(link.into());
		link.click();

If there is interest in including this data-url-anchor style of supporting direct download of bytes to a file, I would be happy to create a PR with a tidy implementation as …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@emilk
Comment options

Answer selected by wmte
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants