简体   繁体   English

无法将大型HTML内容发布到Chrome上的服务器

[英]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. 我正在尝试通过POST请求发送HTML内容,但使用Chrome无法在服务器端传递HTML内容。 While I get the request data in my jsp when using Mozilla. 当我使用Mozilla时,在我的jsp中获得请求数据。 This works in both browsers when the HTML content is small. 当HTML内容较小时,这在两种浏览器中均适用。 I'm generating a PDF with this HTML content using Apache FOP. 我正在使用Apache FOP使用此HTML内容生成PDF。

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. 我将HTML分为多个块,并将它们发布并在jsp中重新加入它们。 This method of doing it also fails with chrome and ie. 使用chrome和ie的这种方法也会失败。

Well inputs in chrome (or maybe whole webkit) has limit of chars - 524288, You can test it by typing in console: chrome中的输入(或整个Webkit)有字符数限制-524288,您可以通过在控制台中输入进行测试:

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

Why not use simple FormData ? 为什么不使用简单的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: 服务器响应生成为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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM