# 文件相关

可直接复制相关代码 在控制台输出结果

# API

名称 描述
base64ToBlob base64 转 blob
base64ToFile base64 转 file
blobToBase64 blob 转 base64
blobToFile blob 转 file
fileToBase64 file 转 base64
fileToBlob file 转 blob
bytesFormat 字节自动转换为:B、KB、MB、GB、TB
downloadByBase64 base64 文件下载
downloadByImgLink 根据图片 imgUrl 下载图片
downloadByImgSelector 根据 img 标签选择器下载图片
bufferFileDownload 后端返回 文件流前端进行下载

# base64ToBlob

base64 转 blob

语法:

rutils.base64ToBlob(base64)

示例:

rutils.base64ToBlob(
    'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAFrSURBVDhPjZJLLwNRFMe7EWInTUmEaGi9pyo0fYSi2pIQZSdsfQsbawtfwEotWvpWTZBYkZA2io2NT1Btqljoy/zde9thWmM4yS9z7p3zS86ZM4rhsB0CXNjBntrAJDQSDIRmwEUqNZQf8lB4Fmq/pYLP/A059wWnoIs4peUunwmG6CISmXs85B6RyWfxXHhBtpBjbN5so3m/V1puPxiDKeaCEGW+jHw5j+JHCSWSb93uoMmtkZY7Dg0wHi9VVWAkMgelh2PzagNWMrPt67v8KSs9Oih2W9Do7mFog1boj+bl5XfSKo14+g4XqTiu00lcphKwxFbQ6TPKy6/FNybXh+1kFSqvXl7meZ4Vu843YCJn59k6HKdrbE10bllZiG7/BBr21Gj1jqKNMEjEX/dMV2WOLVdVsJzeiWvE1Mhqvxnj0QVcPSUZNKd34hoxNTJtif6eKo+eQXNxm/XUyAL9oWmG1DsxkvL/sOMTgE8YwIjgtWoAAAAASUVORK5CYII='
)
// Blob {size: 470, type: "image/png"}

# base64ToFile

base64 转 file

语法:

rutils.base64ToFile(base64, fileName)

示例:

rutils.base64ToFile(
    'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAFrSURBVDhPjZJLLwNRFMe7EWInTUmEaGi9pyo0fYSi2pIQZSdsfQsbawtfwEotWvpWTZBYkZA2io2NT1Btqljoy/zde9thWmM4yS9z7p3zS86ZM4rhsB0CXNjBntrAJDQSDIRmwEUqNZQf8lB4Fmq/pYLP/A059wWnoIs4peUunwmG6CISmXs85B6RyWfxXHhBtpBjbN5so3m/V1puPxiDKeaCEGW+jHw5j+JHCSWSb93uoMmtkZY7Dg0wHi9VVWAkMgelh2PzagNWMrPt67v8KSs9Oih2W9Do7mFog1boj+bl5XfSKo14+g4XqTiu00lcphKwxFbQ6TPKy6/FNybXh+1kFSqvXl7meZ4Vu843YCJn59k6HKdrbE10bllZiG7/BBr21Gj1jqKNMEjEX/dMV2WOLVdVsJzeiWvE1Mhqvxnj0QVcPSUZNKd34hoxNTJtif6eKo+eQXNxm/XUyAL9oWmG1DsxkvL/sOMTgE8YwIjgtWoAAAAASUVORK5CYII=',
    '测试名称'
)
// File {name: "测试名称", lastModified: 1624334190275, lastModifiedDate: Tue Jun 22 2021 11:56:30 GMT+0800 (中国标准时间), webkitRelativePath: "", size: 470, …}

# blobToBase64

blob 转 base64

语法:

rutils.blobToBase64(Blob)

示例:

rutils
    .blobToBase64(
        rutils.base64ToBlob(
            'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAFrSURBVDhPjZJLLwNRFMe7EWInTUmEaGi9pyo0fYSi2pIQZSdsfQsbawtfwEotWvpWTZBYkZA2io2NT1Btqljoy/zde9thWmM4yS9z7p3zS86ZM4rhsB0CXNjBntrAJDQSDIRmwEUqNZQf8lB4Fmq/pYLP/A059wWnoIs4peUunwmG6CISmXs85B6RyWfxXHhBtpBjbN5so3m/V1puPxiDKeaCEGW+jHw5j+JHCSWSb93uoMmtkZY7Dg0wHi9VVWAkMgelh2PzagNWMrPt67v8KSs9Oih2W9Do7mFog1boj+bl5XfSKo14+g4XqTiu00lcphKwxFbQ6TPKy6/FNybXh+1kFSqvXl7meZ4Vu843YCJn59k6HKdrbE10bllZiG7/BBr21Gj1jqKNMEjEX/dMV2WOLVdVsJzeiWvE1Mhqvxnj0QVcPSUZNKd34hoxNTJtif6eKo+eQXNxm/XUyAL9oWmG1DsxkvL/sOMTgE8YwIjgtWoAAAAASUVORK5CYII='
        )
    )
    .then(base64 => {
        console.log(base64)
    })

# blobToFile

blob 转 file

语法:

rutils.blobToFile(Blob, fileName)

示例:

rutils.blobToFile(
    rutils.base64ToBlob(
        'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAFrSURBVDhPjZJLLwNRFMe7EWInTUmEaGi9pyo0fYSi2pIQZSdsfQsbawtfwEotWvpWTZBYkZA2io2NT1Btqljoy/zde9thWmM4yS9z7p3zS86ZM4rhsB0CXNjBntrAJDQSDIRmwEUqNZQf8lB4Fmq/pYLP/A059wWnoIs4peUunwmG6CISmXs85B6RyWfxXHhBtpBjbN5so3m/V1puPxiDKeaCEGW+jHw5j+JHCSWSb93uoMmtkZY7Dg0wHi9VVWAkMgelh2PzagNWMrPt67v8KSs9Oih2W9Do7mFog1boj+bl5XfSKo14+g4XqTiu00lcphKwxFbQ6TPKy6/FNybXh+1kFSqvXl7meZ4Vu843YCJn59k6HKdrbE10bllZiG7/BBr21Gj1jqKNMEjEX/dMV2WOLVdVsJzeiWvE1Mhqvxnj0QVcPSUZNKd34hoxNTJtif6eKo+eQXNxm/XUyAL9oWmG1DsxkvL/sOMTgE8YwIjgtWoAAAAASUVORK5CYII='
    ),
    'fileName'
)
// File {name: "fileName", lastModified: 1624340040360, lastModifiedDate: Tue Jun 22 2021 13:34:00 GMT+0800 (中国标准时间), webkitRelativePath: "", size: 470, …}

# fileToBase64

file 转 base64

语法:

rutils.fileToBase64(file)

示例:

rutils
    .fileToBase64(
        rutils.base64ToFile(
            'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAFrSURBVDhPjZJLLwNRFMe7EWInTUmEaGi9pyo0fYSi2pIQZSdsfQsbawtfwEotWvpWTZBYkZA2io2NT1Btqljoy/zde9thWmM4yS9z7p3zS86ZM4rhsB0CXNjBntrAJDQSDIRmwEUqNZQf8lB4Fmq/pYLP/A059wWnoIs4peUunwmG6CISmXs85B6RyWfxXHhBtpBjbN5so3m/V1puPxiDKeaCEGW+jHw5j+JHCSWSb93uoMmtkZY7Dg0wHi9VVWAkMgelh2PzagNWMrPt67v8KSs9Oih2W9Do7mFog1boj+bl5XfSKo14+g4XqTiu00lcphKwxFbQ6TPKy6/FNybXh+1kFSqvXl7meZ4Vu843YCJn59k6HKdrbE10bllZiG7/BBr21Gj1jqKNMEjEX/dMV2WOLVdVsJzeiWvE1Mhqvxnj0QVcPSUZNKd34hoxNTJtif6eKo+eQXNxm/XUyAL9oWmG1DsxkvL/sOMTgE8YwIjgtWoAAAAASUVORK5CYII=',
            '测试名称'
        )
    )
    .then(Base64 => {
        console.log(Base64)
    })

# fileToBlob

file 转 blob

语法:

rutils.fileToBlob(file)

示例:

rutils
    .fileToBlob(
        rutils.base64ToFile(
            'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAFrSURBVDhPjZJLLwNRFMe7EWInTUmEaGi9pyo0fYSi2pIQZSdsfQsbawtfwEotWvpWTZBYkZA2io2NT1Btqljoy/zde9thWmM4yS9z7p3zS86ZM4rhsB0CXNjBntrAJDQSDIRmwEUqNZQf8lB4Fmq/pYLP/A059wWnoIs4peUunwmG6CISmXs85B6RyWfxXHhBtpBjbN5so3m/V1puPxiDKeaCEGW+jHw5j+JHCSWSb93uoMmtkZY7Dg0wHi9VVWAkMgelh2PzagNWMrPt67v8KSs9Oih2W9Do7mFog1boj+bl5XfSKo14+g4XqTiu00lcphKwxFbQ6TPKy6/FNybXh+1kFSqvXl7meZ4Vu843YCJn59k6HKdrbE10bllZiG7/BBr21Gj1jqKNMEjEX/dMV2WOLVdVsJzeiWvE1Mhqvxnj0QVcPSUZNKd34hoxNTJtif6eKo+eQXNxm/XUyAL9oWmG1DsxkvL/sOMTgE8YwIjgtWoAAAAASUVORK5CYII=',
            '测试名称'
        )
    )
    .then(blob => {
        console.log(blob)
    })

# bytesFormat

文件大小字节格式化成 ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']

语法:

rutils.bytesFormat(bytes)

示例:

rutils.bytesFormat(1024)
// => "1.00 KB"

rutils.bytesFormat(2399624)
// => "2.29 MB"

rutils.bytesFormat(3100399624)
// => "2.89 GB"

rutils.bytesFormat(3100399621124)
// => "2.82 TB"

# downloadByBase64

通过 base64 下载文件

rutils.downloadByBase64(base64, fileName)

示例:

rutils.downloadByBase64(
    'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAYAAAA71pVKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAFrSURBVDhPjZJLLwNRFMe7EWInTUmEaGi9pyo0fYSi2pIQZSdsfQsbawtfwEotWvpWTZBYkZA2io2NT1Btqljoy/zde9thWmM4yS9z7p3zS86ZM4rhsB0CXNjBntrAJDQSDIRmwEUqNZQf8lB4Fmq/pYLP/A059wWnoIs4peUunwmG6CISmXs85B6RyWfxXHhBtpBjbN5so3m/V1puPxiDKeaCEGW+jHw5j+JHCSWSb93uoMmtkZY7Dg0wHi9VVWAkMgelh2PzagNWMrPt67v8KSs9Oih2W9Do7mFog1boj+bl5XfSKo14+g4XqTiu00lcphKwxFbQ6TPKy6/FNybXh+1kFSqvXl7meZ4Vu843YCJn59k6HKdrbE10bllZiG7/BBr21Gj1jqKNMEjEX/dMV2WOLVdVsJzeiWvE1Mhqvxnj0QVcPSUZNKd34hoxNTJtif6eKo+eQXNxm/XUyAL9oWmG1DsxkvL/sOMTgE8YwIjgtWoAAAAASUVORK5CYII=',
    '测试'
)

通过图片的 imgUrl 进行下载

语法:

rutils.downloadByImgLink(imgUrl, fileName, width, height)

示例:

rutils
    .downloadByImgLink('https://dss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1340127580,1407454083&fm=26&gp=0.jpg', '头像')
    .then(res => {
        console.log('下载成功')
    })
    .catch(err => {
        console.log('下载失败')
    })

// 设置下载图标的宽、高
rutils
    .downloadByImgLink('https://dss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1340127580,1407454083&fm=26&gp=0.jpg', '头像', 200, 200)
    .then(res => {
        console.log('下载成功')
    })
    .catch(err => {
        console.log('下载失败')
    })

# downloadByImgSelector

通过 DOM 中的 img 标签,使用选择器下载图片

语法:

rutils.downloadByImgSelector(imgSlector, fileName)

示例:

<body>
    <img id="test" src="https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=251289958,1860898046&fm=26&gp=0.jpg" />
</body>

<script>
    rutils
        .downloadByImgSelector('#test', '测试下载')
        .then(res => {
            console.log('下载成功')
        })
        .catch(err => {
            console.log('下载失败')
        })
</script>

# bufferFileDownload

根据接收文件流进行下载

语法:

/**
 * Buffer下载文件
 * @param {Buffer} buffer
 * @param {String} fileName
 */

示例:

//
// rutils.bufferFileDownload(buffer,fileName)
上次更新: 9/20/2021, 7:52:16 PM