<!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>文件下载</title>
</head>
<body>
<button onclick="down()">点我下载</button>
</body>
<script>
function imgToBase64(imgUrl, callback) {
fetch(imgUrl).then(res => res.blob())
.then(blob => {
const reader = new FileReader();
reader.onloadend = () => {
callback(reader.result);
}
reader.readAsDataURL(blob)
})
}
function down() {
let fileUrl = 'https://img0.baidu.com/it/u=1604010673,2427861166&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=889'
imgToBase64(fileUrl, (blob) => {
let link = document.createElement('a');
link.setAttribute('href', blob);
link.download = "测试";
link.click();
})
}
</script>
</html>
FileReader实现文件下载
发布时间 2023-07-30 18:43:02作者: HuangBingQuan