核心 JavaScript 代码
const uid = Date.now().toFixed().toString();
const url = `http://localhost:7071/gateway-api/anon/test/download?documentPath=esus/api/2022/5/9/5oyH5Y2XIF8gRWxlbWVudA__gdfileflag3c3-419f-9ac8-f4385802.pdf`
fetch(url, {
method: 'GET',
})
.then(response => {
response.blob().then((blob) => {
const a = document.createElement('a');
const downUrl = window.URL.createObjectURL(blob);
a.href = downUrl;
a.download = `导出数据_${uid}.pdf`;
a.click();
window.URL.revokeObjectURL(downUrl);
});
});
完整 HTML 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件下载demo</title>
</head>
<body>
<script>
const uid = Date.now().toFixed().toString();
const url = `http://localhost:7071/gateway-api/anon/test/download?documentPath=esus/api/2022/5/9/5oyH5Y2XIF8gRWxlbWVudA__gdfileflag3c3-419f-9ac8-f4385802.pdf`
fetch(url, {
method: 'GET',
})
.then(response => {
response.blob().then((blob) => {
const a = document.createElement('a');
const downUrl = window.URL.createObjectURL(blob);
a.href = downUrl;
a.download = `导出数据_${uid}.pdf`;
a.click();
window.URL.revokeObjectURL(downUrl);
});
});
</script>
</body>
</html>