若依框架文件下载

前端:

handleDownload().then((blob) => {

const elink = document.createElement("a");

elink.download = 'xxx.xls';

elink.style.display = "none";

elink.href = URL.createObjectURL(blob);

document.body.appendChild(elink);

elink.click();

URL.revokeObjectURL(elink.href);

document.body.removeChild(elink);

})

export function handleDownload() {

  return request({

    url: '/xxx/xxx',

    method: 'get',

    responseType: 'blob'

  })

}

后端:

    @GetMapping("/xxx")
    public void downloadexce(HttpServletResponse response) throws Exception {
       try {
            String fileName = "xxx.xls";
            String downloadPath =  "D:/"+ fileName;

            File file= new File(downloadPath);
            if(!file.exists())
                throw new Exception();

            // 下载名称
            String downloadName = fileName;
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, downloadName);
            FileUtils.writeBytes(downloadPath, response.getOutputStream());
        }
        catch (Exception e)
        {

        }

    }

你可能感兴趣的:(spring)