1. const getBlob = (url) => {
    2. return new Promise((resolve) => {
    3. const xhr = new XMLHttpRequest()
    4. xhr.open('GET', url, true)
    5. xhr.responseType = 'blob'
    6. xhr.onload = () => {
    7. if (xhr.status === 200) {
    8. resolve(xhr.response)
    9. }
    10. }
    11. xhr.send()
    12. })
    13. }
    14. const saveAs = (blob, filename) => {
    15. // for IE
    16. if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    17. window.navigator.msSaveBlob(blob, filename)
    18. } else {
    19. const link = document.createElement('a')
    20. link.href = window.URL.createObjectURL(blob) // 创建对象url
    21. link.download = filename
    22. link.style.display = 'none'
    23. document.body.appendChild(link)
    24. link.click()
    25. document.body.removeChild(link)
    26. window.URL.revokeObjectURL(link.href) // 静态方法用来释放对象url
    27. }
    28. }
    29. export default function (url, filename = '') {
    30. getBlob(url).then((blob) => {
    31. saveAs(blob, filename)
    32. })
    33. }