• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
可樂_Thompson
博客园    首页    新随笔    联系   管理    订阅  订阅
后台管理系统文件三部曲之——第二部曲实现文件的下载

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

//  影像上传下载接口调技术中台的
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: "请传文件路径!",
        });
      }
    },

 

posted on 2020-10-16 11:38  可樂_Thompson  阅读(152)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3