是否可以将字节追加到< a>的href的数据URI。元素点击元素后具有下载属性? [英] Is it possible to append bytes to a data URI at href of <a> element having download attribute after clicking element?

查看:118
本文介绍了是否可以将字节追加到< a>的href的数据URI。元素点击元素后具有下载属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定 html

 < a download =文件。 txthref =data:application / octet-stream,abc>下载< / a> 

可以将字节追加到数据URI href < a> 元素点击< a> 元素,在将结果文件下载到用户文件系统之前?

解决方案

当然!添加修改href属性的onclick处理程序,并在下载开始之前修改它。



  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.txthref =data:application / octet-stream,abc> download< / a>  

p>

此外,重要的是要注意,您不应该为每个字节修改href,因为DOM每次被编辑时都会重新加载。而是修改中间变量,然后将其添加到href作为一个操作。


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?

解决方案

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.

这篇关于是否可以将字节追加到&lt; a&gt;的href的数据URI。元素点击元素后具有下载属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆