Skip to content

Commit

Permalink
Merge pull request #2 from tobe-fe-dalao/main
Browse files Browse the repository at this point in the history
更新
  • Loading branch information
ershisi214 authored Jul 9, 2022
2 parents 683a09a + 7c03e93 commit 67d82bc
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
82 changes: 74 additions & 8 deletions src/dom/file.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/**
* @func dataURLtoFile
* @param {string} dataUrl
* @param {string} fileName
* @desc 将base64转换为文件
* @returns {any}
* @example dataURLtoFile(dataUrl, fileName)
* @param {string} dataUrl
* @param {string} fileName
* @desc 将dataUrl转换为文件
* ie无法使用new File构造函数,建议使用dataURLtoBlob
* @return {any} 文件对象
* @example let file = dataURLtoFile(dataUrl, fileName)
*/
export const dataURLtoFile = (dataUrl: string, fileName: string = getFileName()): any => {
let arr: Array<any> = dataUrl.split(',');
Expand All @@ -17,14 +18,37 @@ export const dataURLtoFile = (dataUrl: string, fileName: string = getFileName())
}
return new File([u8arr], fileName, { type: mime });
}

/**
* @func base64toFile
* @param {string} base64
* @param {string} fileName
* @desc 将base64转换为文件
* @returns {any} 文件对象
* @example let file = base64toFile(base64, fileName)
*/
export const base64toFile = (base64: string, fileName: string = getFileName()): any => {
return dataURLtoFile(base64, fileName)
}
/**
* @func getFileName
* @desc 设置文件名字
* @return { string } fileName 文件名
* @example let fileName = getFileName();
*/
function getFileName() {
let date = new Date();
let fileName = '' + date.getFullYear() + (date.getMonth() < 9 ? +'0' : '')
+ (date.getMonth() + 1) + (date.getDate() < 10 ? +'0' : '') + date.getDate()
+ date.getHours() + date.getMinutes() + (date.getSeconds() < 10 ? '0' : '') + date.getSeconds();
return fileName;
}
/**
* @func dataURLtoBlob
* @param { string } dataurl
* @desc 将dataUrl/base64转换为blob
* @return { Blob } Blob
* @example let blob = dataURLtoBlob(dataurl)
*/
export const dataURLtoBlob = (dataurl: string): Blob | null => {
let mime;
let u8arr;
Expand All @@ -41,9 +65,51 @@ export const dataURLtoBlob = (dataurl: string): Blob | null => {
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([ u8arr ], { type: mime });
return new Blob([u8arr], { type: mime });
}
}
return null;
};

/**
* @func base64toBlob
* @param { string } base64
* @desc 将base64转换为blob
* @return { Blob } Blob
* @example let blob = base64toBlob(dataurl)
*/
export const base64toBlob = (base64: string): Blob | null => {
return dataURLtoBlob(base64)
};
/**
* @func blobtoFile
* @param { Blob } blob
* @param { string } fileName
* @desc 将blob转换为文件
* @return { any } 文件对象
* @example let file = blobtoFile(blob, filename)
*/
export const blobtoFile = (blob: Blob, fileName: string = getFileName()): any => {
return new File([blob], fileName, { type: blob.type })
};
/**
* @func filetoBase64
* @param file 文件对象
* @desc 文件对象转base64
* @return { Promise } Promise对象,异步处理
*/
export const filetoBase64 = (file: any): Promise<any> => {
// return window.URL.createObjectURL(file)
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = (event) => {
if (event.target) {
resolve(event.target.result)
} else {
reject(
console.error(event)
)
}
}
})
}
3 changes: 1 addition & 2 deletions src/function/observe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const defineReactive = (obj: Object, key: string, val: any): void => {
/**
* 设计一个对象的观测者
* @param { Object } obj 对象
* @param { String } key 对象的key
* @return { Object } 返回一个可观测对象
* @example let obj = observerDef({name:'alex',age:18})
*/
Expand Down Expand Up @@ -58,4 +57,4 @@ export const observeProxy = (obj: Object, cal: (val: any) => void) => {
return Reflect.deleteProperty(target, prop)
}
})
}
}

0 comments on commit 67d82bc

Please sign in to comment.