0

I have a really large String that needs to be passed via input tag in a html form from client to server. Until now I've passed it just as a javascript var like this:

document.getElementById("editorText").value = quill.getText();

whereas the upload looks like this:

<form action="/someEndpoint" method="post">
    <input class="btn btn-success btn-lg" style="margin-top: 15px;" type="submit" th:value="#{mSave}"/>
    <textarea style="visibility:hidden" id="editorText" name="editorText"></textarea>
</form>

The problem with this is that the upload does not work this way when the quill.getText() gets too large in size. I'd like to handle the upload via multipart file.

So what I need in this case is javascript code that takes the text from quill.getText(), creates a file (preferably .xml) and servers this file for upload in the html form mentioned above to a controller endpoint looking like this:

@RequestMapping(value = "/someEndpoint", method = RequestMethod.POST)
public String handleFileUpload(final Model model, @RequestParam("editorText") String editorText){

//handling stuff
}

I've tried something along the line of var xml = jQuery.parseXML(quill.getText()) but couldn't quiet get it to work. Whats the simplest way to pack the string into a .xml file for upload?

ps: quill.getText() is referring to this library

Edit 1: Changed the html input to a textfield.

Claudio Brasser
  • 511
  • 3
  • 20

1 Answers1

0

In the code that you have posted, I can see that you are assigning the quill text to a input type text. If you can see the post here, there is a maximum limit for a input type field.

Whereas, A text area can hold an unlimited number of characters. So, can you try creating a hidden textarea <textarea style="visibility:hidden" id="quillText"></textarea> and assign it a value of quill.getText()?

It should give you a complete text on the server side.

EDIT:

The following code worked for me.

UI:

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  <link href="https://cdn.quilljs.com/1.1.6/quill.snow.css" rel="stylesheet">
  <script src="https://cdn.quilljs.com/1.1.6/quill.js"></script>

<form action="submitQuill" method="post">
  <!-- Create the editor container -->
  <input type='submit' onclick="javascript:submitForm()"/>
  <div id="editor">
    <p>Hello World!</p>
    <p>Some initial <strong>bold</strong> text</p>
    <p><br></p>
  </div>
  <textarea style="visibility:hidden" id="editorText" name="editorText"></textarea>

</form>
  <!-- Initialize Quill editor -->
  <script>
    var quill = new Quill('#editor', {
      theme: 'snow'
    });

    function submitForm(){
        var quillText = quill.container.firstChild.innerHTML;
        $("textarea#editorText").val(quillText);
        document.form.submit();
    }

  </script>

On server side :

@RequestMapping(value="/submitQuill")
    public ModelAndView submitQuill(@RequestParam String editorText){
        System.out.println("Editor Text = "+editorText);
        return new ModelAndView("quill");
    }

Output on Console:

Editor Text = <p>Hello World!</p><p>Some initial <strong>bold</strong> text</p><
p><br></p>
amdg
  • 2,211
  • 1
  • 14
  • 25
  • I did not even think about this possibility yet! I tried but could not get it to work, the backend is still throwing the exception that the required parameter is missing. – Claudio Brasser Nov 03 '17 at 11:29
  • Interesting! if you create a textarea like `` in a form and try to get it as `@RequestParam("quillText") String quillText`, what does it give you? – amdg Nov 03 '17 at 11:36
  • I edited the html in my question to the way I changed it according to your hint. Like this I get the following exception in the backend: `org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'editorText' is not present` – Claudio Brasser Nov 03 '17 at 11:45
  • will try to set this 'quill' up in the next week. Never had such a problem while working with online editors. In the mean while, can you perhaps check what is being sent over as a request in chrome development tools? – amdg Nov 04 '17 at 07:50
  • Thanks a lot for the test! This exact setup works for me with smaller text files loaded into the quill editor. Problem occurs only with a really large xml file that has to be handled by the editor. (Notepadd++ Summary: Characters without blanks: 2'421'664 Words: 284'801 Lines: 38'200 Size: 2405 KB It seems like the problem is on client side since I can't even show the request preview in the dev tools, what to me looks like the request was not built correctly. – Claudio Brasser Nov 07 '17 at 12:27