mardi 26 octobre 2021

FileReader. readAsArrayBuffer(fileObject) for getting binary form of image

I'm trying to upload an image from the front end and send it to the API in binary format(only binary is accepted by API). But when I try to send the binary form, the file is uploaded but is corrupted and the image size in the server is always 2B instead of the original size. Below is my code:

<input type="file" accept="image/*" @change="handleImage" />
<button @click="submit()">Submit</button>

function handleImage(e) {
  const selectedImage = e.target.files[0]
  createBase64Image(selectedImage)
},
function createBase64Image(fileObject) {
  const reader = new FileReader()
  reader.onload = (e) => {
    const binaryImage = e.target.result
  }
  reader.readAsArrayBuffer(fileObject)
  }

The binaryImage will hold the raw binary data from the file and upon submit, I'm sending this binaryImage to the API. Please let me know if anyone experienced a similar issue and got this fixed.

Thanks




Aucun commentaire:

Enregistrer un commentaire