zoukankan      html  css  js  c++  java
  • js base64图片下载

    download(base64) {
                    let imgData = base64;//这里放需要下载的base64
                    this.downloadFile('海报.png', imgData);
                },
                downloadFile(fileName, content) {
                    let aLink = document.createElement('a');
                    let blob = this.base64ToBlob(content); //new Blob([content]);
             
                    let evt = document.createEvent("HTMLEvents");
                    evt.initEvent("click", true, true);//initEvent 不加后两个参数在FF下会报错  事件类型,是否冒泡,是否阻止浏览器的默认行为
                    aLink.download = fileName;
                    aLink.href = URL.createObjectURL(blob);
             
                    // aLink.dispatchEvent(evt);
                    aLink.click()
                },
                  //base64转blob
                  base64ToBlob(code) {
                    let parts = code.split(';base64,');
                    let contentType = parts[0].split(':')[1];
                    let raw = window.atob(parts[1]);
                    let rawLength = raw.length;
             
                    let uInt8Array = new Uint8Array(rawLength);
             
                    for (let i = 0; i < rawLength; ++i) {
                      uInt8Array[i] = raw.charCodeAt(i);
                    }
                    return new Blob([uInt8Array], {type: contentType});
                },
  • 相关阅读:
    会场安排
    Comet OJ
    CodeForces1154F
    CodeForces1154E
    2019.08.25校内模拟赛Graph
    2019.08.25校内模拟赛Page
    [MtOI2019]灵梦的计算器
    [MtOI2019]永夜的报应
    [NOI2018]归程
    USACO[CowCoupons]
  • 原文地址:https://www.cnblogs.com/zonglonglong/p/13367782.html
Copyright © 2011-2022 走看看