0

There is a trick for creating files in the client using data uri. For example, to create a MS powerpoint you can do something like

<a href="data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,UEsDBBQACAAIAGeGhT8AAAAAAAAAAAAAAAATAAAAW0NvbnRlbnRfVHlwZXNdLnhtbM2ay27bMBBF9wX6D4K2hUWbbtO0sJNFH6s+AiT9AFYa22wlkiBpN/77UnLaKEFsiNUQnY1smZw7B0MNLwFrcXnb1NkOrJNaLfNZMc0zU..."  download="jones6.pptx"> Test File 3 </a>

Is the only data that can be specified in the in the URL is the base64 or the binary representation of the file? Is it possible to express any other representation of the file?

Rob W
  • 341,306
  • 83
  • 791
  • 678
dublintech
  • 16,815
  • 29
  • 84
  • 115

2 Answers2

3

Both Base64-encoded and plain content is supported. For example, you can use HTML too:

    <a href="data:text/html;charset=UTF-8,%3C!DOCTYPE%20html%3E%3Chtml%3E%3Chead%3E%3Ctitle%3ETest%3C%2Ftitle%3E%3C%2Fhead%3E%3Cbody%3E%3Ch3%3EIt%20works!%3C%2Fh3%3E%3C%2Fbody%3E%3C%2Fhtml%3E">Click Here!</a>

You should see a "It works!" after clicking the link. (Works in Firefox and Chrome, for compatibility table, see http://caniuse.com/datauri) To generate plain content that can be filled into Data URI, you need to use something like encodeURIComponent in JavaScript or rawurlencode in PHP.

littlebtc
  • 466
  • 4
  • 6
  • +1 for the part about url encoding. This is necessary, because there's a slim chance that a `%` sequence appears in the binary data, which is not interpreted literally by the browser, but decoded. – Rob W Aug 28 '12 at 14:54
  • For PowerPoint files, since Both `ppt` and `pptx` are binary files, so I think they can only be represented by base64-encoded string. – littlebtc Aug 28 '12 at 14:58
0

The syntax of the data protocol is as follows:

data:[sMediaType;][sBase64Encoding;],sResourceData

(https://msdn.microsoft.com/library/cc848897)

As you can see, there isn't any option for a non-base64 encoding.

Community
  • 1
  • 1
Waleed Khan
  • 11,426
  • 6
  • 39
  • 70