upload a base64 encoded image using formdata

Solutions on MaxInterview for upload a base64 encoded image using formdata by the best coders in the world

showing results for - "upload a base64 encoded image using formdata"
Miguel Ángel
15 Jul 2019
1 var imgBase64 = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCA..." //your bse64 image
2
3onSubmit(){
4  const file = DataURIToBlob(imgBase64)
5  const formData = new FormData();
6  formData.append('upload', file, 'image.jpg') 
7  formData.append('profile_id', this.profile_id) //other param
8  formData.append('path', 'temp/') //other param
9}
10
11function DataURIToBlob(dataURI: string) {
12  const splitDataURI = dataURI.split(',')
13  const byteString = splitDataURI[0].indexOf('base64') >= 0 ? atob(splitDataURI[1]) : decodeURI(splitDataURI[1])
14  const mimeString = splitDataURI[0].split(':')[1].split(';')[0]
15
16  const ia = new Uint8Array(byteString.length)
17  for (let i = 0; i < byteString.length; i++)
18    ia[i] = byteString.charCodeAt(i)
19
20  return new Blob([ia], { type: mimeString })
21}
22