maudulus
maudulus

Reputation: 11035

Converting to base64 returns a very short string (too short)

I'm submitting a form that contains sometimes single, sometimes multiple images. However when I submit an image, say 1.jpg, the console output from my node server is MS5qcGc=. I assume that somehow the image isn't being sent through, only the text? How can I send through the entire image, and convert it to base64?

Client

<form method="post">
    <div class="form-group">
        <label for="imgs">Images</label><input accept="image/*" multiple name="imgs" type="file">
    </div>
</form>

Server

if (typeof req.body.imgs == "string") {
  console.log(new Buffer(req.body.imgs).toString('base64'));
} else {
  for ( i in req.body.imgs) {
    console.log(new Buffer(req.body.imgs[i]).toString('base64'));
  }
}

Upvotes: 0

Views: 631

Answers (1)

Sam Axe
Sam Axe

Reputation: 33738

You need to add the enctype attribute with a value of multipart/form-data to your form element.

<form method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label for="imgs">Images</label><input accept="image/*" multiple name="imgs" type="file">
    </div>
</form>

Upvotes: 2

Related Questions