更新记录
0.0.5(2024-11-21) 下载此版本
- fix: 修复vue2类型报错问题
0.0.4(2024-10-26) 下载此版本
- fix: 修复空目录的情况
0.0.3(2024-08-29) 下载此版本
- fix: 修复ios uri问题
平台兼容性
Vue2 | Vue3 |
---|---|
√ | √ |
App | 快应用 | 微信小程序 | 支付宝小程序 | 百度小程序 | 字节小程序 | QQ小程序 |
---|---|---|---|---|---|---|
HBuilderX 4.15,Android:支持,iOS:支持,HarmonyNext:不确定 | × | × | × | × | × | × |
钉钉小程序 | 快手小程序 | 飞书小程序 | 京东小程序 |
---|---|---|---|
× | × | × | × |
H5-Safari | Android Browser | 微信浏览器(Android) | QQ浏览器(Android) | Chrome | IE | Edge | Firefox | PC-Safari |
---|---|---|---|---|---|---|---|---|
√ | √ | √ | √ | √ | × | × | × | × |
lime-file-utils
- fileUtils 是一款可以轻松地在文件和 Base64 编码的数据之间进行转换,从而提高开发效率的UTS API工具包
安装
插件市场导入即可
使用
- APP是同步函数,非APP是Promise
import { fileToDataURL, dataURLToFile, processFile, ProcessFileOptions } from '@/uni_modules/lime-file-utils'
const url = ref('')
const src = ref('')
const base64 = `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCAIAAAAP3aGbAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAH40lEQVR4nO3dwXFjSRIFweba6K9yrwisQ05OBuAuAPABgmF1eVY/f//+/QNQ8L//+gEAXgkWkCFYQIZgARmCBWQIFpAhWECGYAEZggVkCBaQIVhAhmABGYIFZAgWkCFYQIZgARmCBWQIFpAhWECGYAEZggVkCBaQIVhAxj9TL/Tz8zP1UkUv1zu+fEXF13m0+dgvrn3VH2zwV+SEBWQIFpAhWECGYAEZggVkCBaQIVhAhmABGYIFZAgWkCFYQMbYlvDF4KRozeAKbOrjb47XDn78KctTyhfXvqIXyzNJJywgQ7CADMECMgQLyBAsIEOwgAzBAjIEC8gQLCBDsIAMwQIyVreELzanSQenW5sDt+WPv3mf4IvoXYFf/g/ihAVkC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ECMgQLyBAsIEOwgIyxLSHAv80JC8gQLCBDsIAMwQIyBAvIECwgQ7CADMECMgQLyBAsIEOwgAzBAjIEC8gQLCBDsIAMwQIyBAvIECwgQ7CADMECMgQLyBAsIEOwgAzBAjL+DwEJMVCBletEAAAAAElFTkSuQmCC`
// #ifdef WEB || MP
fileToDataURL('/static/logo.png').then(res => {
url.value = res
})
dataURLToFile(base64).then(res => {
src.value = res
})
// #endif
// #ifdef APP
url.value = fileToDataURL('/static/logo.png') ?? ''
src.value = dataURLToFile(base64) ?? '';
// #endif
// 相当于 fileToDataURL
processFile({
type: 'toDataURL',
path: '/static/logo.png',
success: (res: string)=>{
url.value = res
}
} as ProcessFileOptions)
// 相当于 dataURLToFile
processFile({
type: 'toFile',
path: base64,
success: (res: string)=>{
src.value = res
}
} as ProcessFileOptions)
fileToDataURL
将文件
或图片
转成 URL(data URL)
,接收一个文件路径,APP 返回的是DataURL
或null
, 非APP 返回的是Promise<string>
fileToDataURL(filePath : string)
fileToBase64
将文件
或图片
转成 Base64
, 接收一个文件路径,APP 返回的是Base64
或null
, 非APP 返回的是Promise<string>
fileToBase64(filePath : string)
dataURLToFile
将 Base64
编码的数据 URL(data URL)
保存为临时路径,接收一个dataURL,参数filename
为可选, APP返回的是string
或null
,非APP 返回的是Promise<string>
dataURLToFile(dataURL : string, filename : NullableString = null)
processFile
是上面三个函数的总和,接收ProcessFileOptions
processFile({
type: 'toBase64' | 'toDataURL' | 'toFile',
path: string,
filename?: string,//如果是toFile,则可以设置保存文件的文件名
success ?: (res : string) {},
fail ?: (res : any) {},
complete ?: (res : any) {}
} as ProcessFileOptions)
打赏
如果你觉得本插件,解决了你的问题,赠人玫瑰,手留余香。