xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

HTML5 auto download blob file All In One

HTML5 auto download blob file All In One

const obj = {
  id: 2012,
  msg: "hello, world",
  date: new Date(),
}

const alink = document.querySelector('#alink');

alink.addEventListener('click', () => {
    const json = JSON.stringify(obj);
    // binary stream Blob ✅
    const blob = new Blob(
      [json],
      {type: "octet/stream"}
    );
    const url = window.URL.createObjectURL(blob);
    alink.href = url;
    alink.target = '_blank';
    // target filename
    alink.download = 'html5-blob-file-download.json';
});

HTML5 Simple File Blob Download

demos


// <a id="myButton" href="#">Download JSON</button>
// <button id="btn">Download JSON</button>

// dummy json data to save
const data = {
    x: 42,
    s: "hello, world",
    d: new Date()
};

let btn = document.getElementById("btn");

btn.addEventListener(`click`, (e) => {
    let json = JSON.stringify(data),
        // Blob
        blob = new Blob([json], {type: "octet/stream"}),
        // blob to url
        url = window.URL.createObjectURL(blob);
    this.href = url;
    this.download = "api.json";
    this.target = "_blank";
    // target filename
});


demos

  1. json

image

  1. image / video

demos

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs

using HTML5 a tag download attribute for downloading the Blob URL file All In One

https://www.cnblogs.com/xgqfrms/p/16164626.html

https://stackoverflow.com/questions/19327749/javascript-blob-filename-without-link

https://stackoverflow.com/questions/6165266/downloading-mp3-files-using-html5-blobs-in-a-chrome-extension



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(806)  评论(3编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2017-11-07 prototype chain & prototype & __proto__
点击右上角即可分享
微信分享提示