【无标题】

导出文件

export function downloadFileByBlob(file, fileName) {
    const fileBlob = new Blob([file]);
    const objUrl = URL.createObjectURL(fileBlob);
    const a = document.createElement("a");
    a.style.display = "none";
    a.href = objUrl;
    a.download = fileName;
    a.click();
    URL.revokeObjectURL(objUrl) // 调用这个方法来让浏览器知道不用在内存中继续保留对这个文件的引用了。
}
export function downloadFile(url, fileName='Log.xlsx', config) {
    
    return request({
        url: url,
        method: 'get',
        responseType: 'blob',
        ...config
    })
    // .then(res=> {
    //     console.log(res)
    // })
    .then(blob => {
        const ele = document.createElement('a')
        const objUrl = URL.createObjectURL(blob);
        ele.href = objUrl;
        ele.download = fileName;
        ele.click()
        URL.revokeObjectURL(objUrl)
    })
}
export function downloadFileByUrl(path, fileName) {
    if(!path) {
        throw "File path is required"
    }
    const arr = path.split('/')
    const defaultName = arr[arr.length - 1].split('?')[0]
    const ele = document.createElement('a')
    ele.setAttribute('href', path)
    ele.setAttribute('target', `_blank`)
    ele.setAttribute('download', fileName || defaultName)
    ele.click()
}
export function downloadImgByBlob(url, name) {
    var img = new Image()
    img.onload = function() {
        var canvas = document.createElement('canvas')
        canvas.width = img.width
        canvas.height = img.height
        var ctx = canvas.getContext('2d')
        // 将img中的内容画到画布上
        ctx.drawImage(img, 0, 0, canvas.width, canvas.height)
        // 将画布内容转换为Blob
        let type = 'image/png'
        if(name){
            const typeObj = {
                'png': 'image/png',
                'jpg': 'image/jpeg'
            }
            let arr = name.split('.')
            type = typeObj[arr[arr.length - 1]]
        }
        canvas.toBlob((blob) => {
            // blob转为同源url
            var blobUrl = window.URL.createObjectURL(blob)
            // 创建a链接
            var a = document.createElement('a')
            a.href = blobUrl
            a.download = name || ''
            // 触发a链接点击事件,浏览器开始下载文件
            a.click()
        }, type)
    }
    img.src = url
    // 必须设置,否则canvas中的内容无法转换为blob
    img.setAttribute('crossOrigin', 'Anonymous')
}

export function uploadFile(file, url, fileKey, config) {
    fileKey = fileKey || 'file'
    const formData = new FormData()
    if (file instanceof Array) {
        for (const key in file) {
            formData.append(fileKey, file[key])
        }
    } else {
        formData.append(fileKey, file)
    }
    return request({
        url: url,
        method: 'post',
        data: formData
    })
}

// url转blob下载
export function downloadFileUrlToBlob(url, fileName) {

    const xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'blob';
    xhr.onload = function() {
        if (xhr.status === 200) {
            downloadFileByBlob(xhr.response, fileName)
        }
    };
    xhr.send();

}

export const downloadBuffer  = ({name=Date.now(), code, type}) => {
    const b = new Blob(code, type && type)
    var a=document.createElement("a");
    a.href=URL.createObjectURL(b)
    a.download=`${name}`;
    a.click();
    URL.revokeObjectURL(b);
}
export function encode(str) {
    let base64 = btoa(str)
    return base64
}
export function decode(base64) {
    let str = atob(base64)
    return str
}

// file转base64
export function file2Base64(file) {
    return new Promise((resolve, reject) => {
        const reader = new FileReader()
        reader.readAsDataURL(file)
        reader.onload = () => resolve(reader.result)
        reader.onerror = (error) => reject(error)
    })
}
  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值