<!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>Document</title>
</head>
<body>
<input type="file">
<button>保存文件</button>
</body>
<script>
const input = document.querySelector('input')
let url = ''
input.onchange = () => {
url = URL.createObjectURL(input.files[0])
console.log(url, url.file, '----url')
}
const btn = document.querySelector('button')
btn.addEventListener('click', () => {
if (!url) return false
saveFile(url, 'test')
})
const saveFile = (url, fileName) => {
const link = document.createElement('a')
link.href = url
link.download = `${fileName}_${Date.now()}`;
link.style.display = 'none';
document.body.appendChild(link)
link.click()
URL.revokeObjectURL(url)
document.body.removeChild(link)
}
</script>
</html>