web-report/utils/base64.ts
2023-08-21 15:36:10 +08:00

24 lines
797 B
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export function base64ToBuffer(data: string) {
//去掉url的头并转换为byte
const split = data.split(',');
const bytes = window.atob(split[1]);
//处理异常,将ascii码小于0的转换为大于0
const ab = new ArrayBuffer(bytes.length);
const ia = new Uint8Array(ab);
for (let i = 0; i < bytes.length; i++) {
ia[i] = bytes.charCodeAt(i);
}
const match = split[0].match(/^data:(.*);base64/)
const contentType = match && match.length == 2 ? match[1] : ''
//return new Blob([ab], {type: split[0]});
return {
buffer: [ab],
contentType
}
}
export function base64ToFile(data: string, fileName: string) {
const _data = base64ToBuffer(data);
return new File(_data.buffer, fileName, {type:_data.contentType})
}