Answers for "upload base64 iamge with formdata"

1

Upload a base64 encoded image using FormData

var imgBase64 = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCA..." //your bse64 image

onSubmit(){
  const file = DataURIToBlob(imgBase64)
  const formData = new FormData();
  formData.append('upload', file, 'image.jpg') 
  formData.append('profile_id', this.profile_id) //other param
  formData.append('path', 'temp/') //other param
}

function DataURIToBlob(dataURI: string) {
  const splitDataURI = dataURI.split(',')
  const byteString = splitDataURI[0].indexOf('base64') >= 0 ? atob(splitDataURI[1]) : decodeURI(splitDataURI[1])
  const mimeString = splitDataURI[0].split(':')[1].split(';')[0]

  const ia = new Uint8Array(byteString.length)
  for (let i = 0; i < byteString.length; i++)
    ia[i] = byteString.charCodeAt(i)

  return new Blob([ia], { type: mimeString })
}
Posted by: Guest on March-04-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language