Character limit uploading base64 image to nodejs server

I’m trying to upload an image (in base64 format) to a node server through element in html:

<form action="/" method="POST">
  <input type="file" id="image_input" accept="image/jpeg, image/png, image/jpg" multiple=true> 
  <input type=hidden id=images1 name=images>
<input type="submit">
const image_input = document.querySelector("#image_input");

image_input.addEventListener("change", function() {
  const file_reader = new FileReader();
  file_reader.addEventListener("load", () => {
    const uploaded_image = file_reader.result;
    	document.getElementById("images1").value = uploaded_image

Node.JS:"/", (req,res)=>{

It throws this error:

PayloadTooLargeError: request entity too large

Is there any way to increase how many characters I can send with post? Or is there a more compressed image format I can use instead?

Is there any reason you need to send the image as base64 data? multipart/form-data is kinda better in this situation and you probably won’t have any problems with limit uploading.

You can use multer as middleware

const multer = require('multer');
const upload = multer();"/", upload.single('image_input'), (req, res) => {
  // just so you know that req.file contains information about the uploaded file

That way you don’t have to convert the image to base64 anymore, besides being more effective too.


Do you know how to decode the file once multer uploads it to a file? I’m getting this:


How do I turn that into an image file?

1 Like

This is how the file in raw binary looks like, you just need to write it to an image file. We can use fs module’s writeFile function to write the data.

const express = require('express');
const multer = require('multer');
const fs = require('fs');

const app = express();
const upload = multer();"/", upload.single('image_input'), (req, res) => {
    // Make a little check to see if the file is present
    if (!req.file) {
        return res.status(400).send('No file uploaded.');

    // Than you write the file
    fs.writeFile('uploaded_image.jpg', req.file.buffer, function(err) {
        if (err) {
            return res.status(500).send(err);

        res.send('File uploaded successfully.');

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.