24 lines
797 B
TypeScript
24 lines
797 B
TypeScript
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})
|
||
}
|