Search code examples
javajavascriptgoogle-chromefirefoxinternet-explorer-10

Unable to POST large html content to server on Chrome


I'm trying to send HTML content through a POST request but it is not getting delivered on the server side using Chrome. While I get the request data in my jsp when using Mozilla. This works in both browsers when the HTML content is small. I'm generating a PDF with this HTML content using Apache FOP.

var iframe = document.createElement('iframe');
iframe.setAttribute('name','eyeframe');
document.body.appendChild(iframe);
var myform = document.createElement('form');
document.body.appendChild(myform);
myform.setAttribute('action','myJspToRenderHtmlAsPdf.jsp');
myform.setAttribute('method','post');
myform.setAttribute('target','eyeframe');

var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", "htmlContent");
hiddenField.setAttribute("value", strHTML);
myform.appendChild(hiddenField);
myform.submit();

I am dividing the HTML into chunks and posting them and rejoining them in the jsp. This method of doing it also fails with chrome and ie.


Solution

  • Well inputs in chrome (or maybe whole webkit) has limit of chars - 524288, You can test it by typing in console:

    var el = document.createElement("input");
    el.maxLength
    

    Why not use simple FormData?

    var formData = new FormData();
    formData.append("htmlContent", strHTML);
    var request = new XMLHttpRequest();
    request.open("POST", "myJspToRenderHtmlAsPdf.jsp");
    request.send(formData);
    

    Got some time to write simple demo. Server respons is generated PDF:

    <a href="#" id="test">Download</a>
    <script>
    
    var formData = new FormData();
    formData.append("htmlContent", 'test');
    var request = new XMLHttpRequest();
    request.responseType = 'blob';
    request.open("POST", "myJspToRenderHtmlAsPdf.jsp");
    
    request.onload = function(event) {
        if (request.status == 200) {
          var blob = new Blob([request.response], {type: 'application/pdf'});
          var url = URL.createObjectURL(blob);
          var link = document.querySelector('#test');
          link.setAttribute('href', url);
        } else {
          // Handle error
        }
      };
    
    request.send(formData, true);
    </script>