-2

I'm using Node.js/Express and submitting an image to the server from a webpage via AJAX. The image has been processed and cropped, so I'm using the resultant dataURL and sending it to my server by a post request like this. The dataURL is completely good and accurate and contains the image as it should. So I simply send it to the server.

  var fd = new FormData();
  fd.append('dataURL',dataURL);
  console.log('dataURL length:',dataURL.length);
  $.ajax({
      type: "POST",
      url: `/pwa/update-attachment`,
      data: fd,
      contentType: false,
      processData: false, 
      success: function(result)
      {
      }
    });

On the server side there are no errors but the dataURL parameters is truncated to 1mb. For reference, my server is setup to accept multipart form data using using bodyparser as follows. I tried using size limits with bodyParser (either high or low) and neither seem to take effect in this case.

const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false })); // support encoded bodies

I receive my post like this:

 router.post('/update-attachment',(req,res) => {
    console.log('dataurl:',req.body.dataURL.length);
    ...
 });

The dataURL is truncated to 1048576 bytes. Works perfectly fine for smaller images.

David J
  • 1,018
  • 1
  • 9
  • 14
  • 1
    Take a look at the [documentation](https://www.npmjs.com/package/body-parser#limit-3). – Jake Holzinger Aug 26 '19 at 22:37
  • I've actually reviewed the limit parameter but it doesn't seem to apply here. That's why I think there is something else that I'm unaware of going on. I actually in other contexts have received the "entity too large" error, but whether I make the limit high or arbitrarily low (so it will fail and I can show the parameter is being recognized), it doesn't seem to have in impact here. – David J Aug 27 '19 at 17:49

1 Answers1

0

The reason that there is not an error is that it is being truncated on the client side by the browser. To send the image, it should not be sent as a dataURL, but as a blob. The following answer explains this:

Convert Data URI to File then append to FormData

David J
  • 1,018
  • 1
  • 9
  • 14