<!DOCTYPE html>
<html>
<head>
        <title></title>
</head>
<body>

<button type="button" onclick="download()" >点击下载</button>


<script type="text/javascript">
        function download() {
    var url = 'https://cotbmusic.oss-cn-shenzhen.aliyuncs.com/upload_pic/202067/a.mp3';
    let name = 'a.mp3';
    // 发送http请求,将文件链接转换成文件流
    fileAjax(url, function(xhr) {
        downloadFile(xhr.response, name)
    }, {
        responseType: 'blob'
    })
}

function fileAjax(url, callback, options) {
    let xhr = new XMLHttpRequest()
    xhr.open('get', url, true)
    if (options.responseType) {
        xhr.responseType = options.responseType
    }
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4 && xhr.status === 200) {
            callback(xhr)
        }
    }
    xhr.send()
}

function downloadFile(content, filename) {
    window.URL = window.URL || window.webkitURL
    let a = document.createElement('a')
    let blob = new Blob([content])
// 通过二进制文件创建url
    let url = window.URL.createObjectURL(blob)
    a.href = url
    a.download = filename
    a.click()
// 销毁创建的url
    window.URL.revokeObjectURL(url)
}
</script>
</body>
</html>
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐