フロントエンド ダウンロード ファイル、BLOB 形式のダウンロードに変換された Base64 データ

ダウンロード要件が発生すると、バックエンドはファイルを Base64 データに変換し、フロントエンドに返します。次に、Base64 データを BLOB 形式に変換してフロントエンドにダウンロードする方法について説明します。

 

downloadAttachment({ attachmentId: id })
    .then(({ data }) => {
      proxy.$modal.closeLoading();
      const raw = window.atob(data.content);
      const rawLength = raw.length;
      const uInt8Array = new Uint8Array(rawLength);
      for (let i = 0; i < rawLength; ++i) {
        uInt8Array[i] = raw.charCodeAt(i);
      }
      const blob = new Blob([uInt8Array], { type: data.contentType });
      const elink = document.createElement("a");
      elink.download = data.attachName;
      elink.style.display = "none";
      elink.href = URL.createObjectURL(blob);
      document.body.appendChild(elink);
      elink.click();
    })
    .catch(() => {
      proxy.$modal.closeLoading();
    });

おすすめ

転載: blog.csdn.net/SunFlower914/article/details/130731533