guest271314 guest271314 - 2 months ago 8
HTML Question

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

Given

html


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


can bytes be appended to
data URI
at
href
of
<a>
element after click on
<a>
element, before resulting file is downloaded to user filesystem?

Answer

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.href+=data;
}
<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.

Comments