首页 > 技术文章 > 后台管理系统文件三部曲之——第二部曲实现文件的下载

Ky-Thompson23 2020-10-16 11:38 原文

文件下载的实现,这个点在做的时候调用到了技术中台的接口

//  影像上传下载接口调技术中台的
export function getDownUrl(objectId) {
    return requestExport({
      url: `/tc-storage-service/obj/storage/download?objectId=${objectId}`,
      method: 'get'
    })
  }

js代码实现

//  下载影像上传的文件
    downLoad(id, name) {
      const fileName = name;
      if (id) {
        getDownUrl(id)
          .then((res) => {
            const link = document.createElement("a");
            const blob = new Blob([res]);
            var reader = new FileReader();
            reader.readAsText(blob, "utf-8");
            reader.onload = () => {
              if (window.navigator && window.navigator.msSaveOrOpenBlob) {
                navigator.msSaveBlob(blob);
              } else {
                link.style.display = "none";
                link.href = URL.createObjectURL(blob);
                link.setAttribute("download", fileName);
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
              }
            };
          })
          .catch((error) => {
            console.log(error);
          });
      } else {
        this.$message({
          type: "error",
          message: "请传文件路径!",
        });
      }
    },

 

推荐阅读