前端使用XMLHttpRequest实现通过链接下载图片
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="button" value="test" onclick="test()">
<script>
const fileInfo = {
url: 'https://res.hc-cdn.com/cnpm-common-resource/2.0.2/base/header/components/images/logo.png',
name: 'a.png'
}
function test() {
downloadUrlFile(fileInfo.url)
}
function downloadUrlFile(url) {
const xhr = new XMLHttpRequest()
xhr.open('GET', url, true)
xhr.responseType = 'blob'
xhr.onload = () => {
if (xhr.status === 200) {
saveAs(xhr.response, fileInfo.