文件处理
框架提供了一些常用的处理文件方法可以直接使用。
getFileName
根据文件路径获取文件名。
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| path | 文件路径 | string | — |
ts
import { getFileName } from 'liv-web';
const path = '/server/file/test.png';
const fileName = getFileName(path);
console.log(fileName); // 输出test.pnggetMd5ByFile2.1.0
通过文件获取唯一标识md5
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| file | 文件对象 | File | — |
ts
import { getMd5ByFile } from 'liv-web';
const file = new File([], 'test.txt');
console.log(getMd5ByFile(file)); // 文件的MD5值downloadFileByUrl2.1.1
通过url地址下载文件。
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| url | 需要下载文件的url地址 | string | — |
ts
import { downloadFileByUrl } from 'liv-web';
downloadFileByUrl('https://www.liv-web.com/download/test.txt');base64ToBlob2.2.0
base64字符串转blob对象。
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| base64 | 需要转换的base64字符串 | string | — |
| type | 媒体类型 | string | 默认从base64字符串中获取 |
ts
import { base64ToBlob } from 'liv-web';
const base64 = 'data:image/png;base64,iVBORw...';
base64ToBlob(base64);blobToBase642.2.0
blob对象转base64字符串。
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| blob | 需要转换的blob对象 | Blob | — |
ts
import { blobToBase64 } from 'liv-web';
const blob = new Blob(['hello world']);
blobToBase64(blob);