How does this macro work to download a binary file?

133 views
Skip to first unread message

leeand00

unread,
Jul 14, 2018, 11:48:56 AM7/14/18
to TiddlyWiki
\define download-xlsx-tiddler(title,caption)
<a href={{{ [[data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,]addsuffix{$title$}] }}} download="$title$">$caption$</a>
\end




It's called like this:

<<download-xlsx-tiddler "Presidents and Justices.xlsx" "Presidents and Justices.xlsx">>




I see that it mentions the MIME type of the and the base64 format is also mentioned, but I don't think I really understand it.  It looks like it's using transclusion and a filter, but I don't really understand the "data:" portion and where the documentation for that is...but it's really neat as it means you can store any sort of file in a Tiddlywiki 5 and download it with a link.

Thanks!

Jed Carty

unread,
Jul 14, 2018, 12:03:50 PM7/14/18
to TiddlyWiki
One way browsers get around handling incompatible data types is to convert them into a base 64 representation that uses only basic ascii characters. Tiddlywiki encodes non-text tiddlers in the same way. Browsers can, give the correct mime type, treat the base64 encoded string as though it were binary data for common data types. So to download the binary versions of the base64 encoded file you can create a download link that points to the base64 encoded string with the proper prefix describing the mime type and encoding and it will download the binary file. The macro creates a link that does exactly that, so when you click on it you get prompted to download the file.

leeand00

unread,
Jul 20, 2018, 2:18:47 AM7/20/18
to tiddl...@googlegroups.com

So,

<a href={{{ [[data:<MIME-TYPE>;<Binary-ENCODING>,]addsuffix{<tiddler-title>}] }}} download="<tiddler-title>"><link-title></a>

is what it really means?  Am I correct?  Apologies, I guess I wasn't clear that this question was about the syntax.  But thank you, your answer did clear it up for me.  Is there any documentation regarding the data: syntax?

Jed Carty

unread,
Jul 20, 2018, 2:50:23 AM7/20/18
to TiddlyWiki
It isn't a tiddlywiki thing, that is just how anchor tags work. I imagine that MDN or W3C has some information about it.

leeand00

unread,
Jul 21, 2018, 6:47:30 AM7/21/18
to tiddl...@googlegroups.com
Oh so I'm barking up the wrong tree!  Thanks for clearing that up and pointing me to the correct documentation.  I wasn't aware that I was looking in the wrong documentation.
Reply all
Reply to author
Forward
0 new messages