vue项目中实现下载功能

朋友们,今天工作碰到要实现下载文件,这个之前没写过,怎么实现的呀,是后端写好的接口,调接口就可以实现了嘛

img

后端写接口,前端发请求就可以了

后端写好接口 axios下载

downloadFile(url, filename) {
  axios({
    url: url,
    method: 'GET',
    responseType: 'blob',
  }).then(response => {
    const url = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', filename);
    document.body.appendChild(link);
    link.click();
  });
}