12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- /*
- * @Author: 范非苗
- * @Date: 2021-03-12 11:02:38
- * @LastEditTime: 2021-03-12 11:05:07
- */
- // 文件下载
- export const downloadFile = function (url,name) {
- const link = document.createElement('a')
- link.setAttribute('download', name)
- link.href = url
- link.click()
- }
- export const download=function (url,filename="文件下载") {
- getBlob(url, function (blob) {
- saveAs(blob, filename);
- });
- }
- function getBlob(url, cb) {
- var xhr = new XMLHttpRequest();
- xhr.open('GET', url, true);
- xhr.responseType = 'blob';
- xhr.onload = function () {
- if (xhr.status === 200) {
- cb(xhr.response);
- }
- };
- xhr.send();
- }
- function saveAs (blob,filename) {
- if(window.navigator.msSaveOrOpenBlob){
- navigator.msSaveBlob(blob,filename)
- }else{
- let a=document.createElement("a")
- let body=document.querySelector("body")
- a.href=window.URL.createObjectURL(blob)
- a.download=filename
- a.style.display="none"
- body.appendChild(a)
- a.click()
- body.removeChild(a)
- window.URL.revokeObjectURL(a.href)
- }
- }
|