HLJ 发布于
2022-03-04 10:29:11

Blob构造函数下载文本内容

上一篇文章:

js面试题

下一篇文章:

Blob动态下载base64图片

点击a标签下载

<a download="hello.txt" href='#' id="link">Download</a>

<script>
   let blob = new Blob(["Hello, world!"], {type: 'text/plain'}); 
   link.href = URL.createObjectURL(blob);
</script>

动态创建a标签下载

let link = document.createElement('a');
link.download = 'hello.txt';

let blob = new Blob(['Hello, world!'], {type: 'text/plain'});

let reader = new FileReader();
reader.readAsDataURL(blob); // converts the blob to base64 and calls onload

reader.onload = function() {
    link.href = reader.result; // data url
    link.click();
};
let link = document.createElement('a');
link.download = 'hello.txt';

let blob = new Blob(['Hello, world!'], {type: 'text/plain'});

link.href = URL.createObjectURL(blob);

link.click();

URL.revokeObjectURL(link.href);

来源:https://javascript.info/blob

当前文章内容为原创转载请注明出处:http://www.good1230.com/detail/2022-03-04/577.html
最后生成于 2023-06-27 21:38:09
上一篇文章:

js面试题

下一篇文章:

Blob动态下载base64图片

此内容有帮助 ?
0