Skip to content

Commit

Permalink
🐛 FIX: 处理 delBase64WithPrefix 正则匹配 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Aomd committed Apr 24, 2024
1 parent 7ef8933 commit e3c0570
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wacky-idea/web-utils",
"version": "1.0.1",
"version": "1.0.2",
"author": "Aomd <[email protected]>",
"main": "dist/cjs/index.js",
"module": "dist/es/index.js",
Expand Down
16 changes: 12 additions & 4 deletions src/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,23 @@ export function fileToBase64(file: File): Promise<string> {

/**
* blob 转 base64
* @param blob
* @returns
*
* @export
* @param {Blob} blob
* @param {boolean} [delPrefix=false]
* @return {*} {Promise<string>}
*/
export function blobToBase64(blob: Blob): Promise<string> {
export function blobToBase64(blob: Blob, delPrefix = false): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader();

reader.onload = () => {
const base64 = delBase64WithPrefix(reader.result as string)
let base64 = ''
if (delPrefix) {
base64 = delBase64WithPrefix(reader.result as string)
} else {
base64 = reader.result as string
}
resolve(base64);
};

Expand Down
2 changes: 1 addition & 1 deletion src/Url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export function base64ToBlob(base64WithPrefix: string, mimeType: string) {
*/
export function delBase64WithPrefix(base64WithPrefix: string) {
// 匹配并提取不带"data"前缀的Base64部分
const regex = /^data:[^;]+;base64,([^/]+)/;
const regex = /^data:[^;]+;base64,([^\"\']*)/;
const matches = base64WithPrefix.match(regex);
if (matches && matches.length > 1) {
return matches[1]
Expand Down

0 comments on commit e3c0570

Please sign in to comment.