How to send a large datauri of an image to express server

1.5k Views Asked by At

I have the daturi of an image which is uploaded from the desktop.I would like to send this data uri to express server so as to save the dataUri in a text file. Since the size of the data uri of the image is quite large I am getting payload too large error which is understandable. I tried using multer but I couldn't figure out how to extract the data uri of the image when multer is used, on the server side.Any help on this is greatly appreciated. Below is some of the code sample that I am trying to use

   <div class="row">
    <div class="form-group">
      <label  class="btn btn-default btn-file" for="FileUpload">Upload a Plan</label>
      <input type="file"  id ="FileUpload" accept="image/*" capture="camera" value="" onchange="readFileURL(this);" style="display: none;">
       <img id="chosenFile" src="#" style="visibility: hidden;"/>
    </div>

  </div>
  <div class="row">
  <div class="col-sm-12"><button style="background-color: green" class="btn btn-default btn-sm" onclick="handleUplod(this)">Upload</button></div>
  </div>
<script type="text/javascript">
  function readFileURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        document.getElementById("chosenFile").style.visibility="visible";
        reader.onload = function (e) {
            $('#chosenFile').attr('src', e.target.result).width(150).height(150);
              console.log("result:",e.target.result);
              imageData = e.target.result;
        };
          console.log("data url:"+reader.readAsDataURL(input.files[0]));

    }
};

function handleUplod(){
  $.ajax({
            type: "POST",
            url: "/send",
            data: { MyPlanDataUri:imageData },
            success: function(result){
              location.href= "/someplace";
            },
          error: function(result) {
              alert('error');
          }
      });
};

On the server side I am doing the following

 app.post('/send', function(req,res) {
     var Tex1 = req.body.MyPlanDataUri;
     var newFile = "ImageFile.txt";
    fs.writeFile(newFile, Tex1, (err) => {
        if (err) res.send(err);
        console.log('File saved successfully ! - ', newFile);
    }
    );
res.send("Successfull");

}

P.S the above code works perfectly fine for small datauri's

0

There are 0 best solutions below