vue 下载本地模板,路径获取不到?

我的Excel文件放到public下:
image.png
然后方法里调用下载:

downloadFile() {
      let a = document.createElement("a");
      a.href = './static/fileTemplate.xlsx';
      console.log(a.href,'href---------');
      a.setAttribute('download', '文件模板.xlsx');
      a.style.display = 'none';
      document.body.appendChild(a);
      a.click();
      a.remove();
    },

控制台打印出来href是这样 :
image.png
下载的时候就报找不到文件
image.png
我觉得正常应该是这样才对:
http://localhost:9102/static/fileTemplate.xlsx
这样就可以下载到本地文件。

不知道哪里出错了,求指教!

阅读 986
1 个回答
a.href = location.origin + '/static/fileTemplate.xlsx';

这样部署到服务器也可以下载文件
这样试试

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题