Useful methods for file-related functions.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
575 B

  1. type DataUriToBlob = (dataUri: string, name?: string) => Blob
  2. const dataUriToBlob: DataUriToBlob = (dataUri, name) => {
  3. const [encoding, base64] = dataUri
  4. .slice('data:'.length)
  5. .split(',')
  6. const binary = atob(base64)
  7. const [type,] = encoding.split(';')
  8. const ab = new ArrayBuffer(binary.length)
  9. const ia = new Uint8Array(ab)
  10. for (let i = 0; i < binary.length; i++) {
  11. ia[i] = binary.charCodeAt(i)
  12. }
  13. if (typeof name! === 'string') {
  14. return new File([ab], name, { type, })
  15. }
  16. return new Blob([ab], { type, })
  17. }
  18. export default dataUriToBlob