Front-end download file, base64 data converted to blob format download

When encountering download requirements, the backend converts the file into base64 data and returns it to the frontend. The following describes how to convert the base64 data into blob format and download it on the frontend:

 

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();
    });

Guess you like

Origin blog.csdn.net/SunFlower914/article/details/130731533