guest271314 guest271314 - 1 year ago 94
HTML Question

Is it possible to append bytes to a data URI at href of <a> element having download attribute after clicking element?



<a download="file.txt" href="data:application/octet-stream,abc">download</a>

can bytes be appended to
data URI
element after click on
element, before resulting file is downloaded to user filesystem?

Answer Source

Certainly! Add an onclick handler that modifies the href attribute, and it will be modified before the download starts.

var a = document.querySelector("a");
a.onclick = function() {
  let data = "";
  for (let i = 0;i < 200000;i++){
    data += i;
<a download="file.txt" href="data:application/octet-stream,abc">download</a>

Also, it's important to note that you should not modify the href for every byte, as the DOM reloads each time that it is edited. Instead, modify an intermediate variable and then add it to the href as one operation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download