0

Here is my code to upload document in liferay via jsonapi. i am adding file inside document liberary. But it show error "No JSON web service action associated with path /dlapp/add-file-entry". I am using liferay 6.2.4 ga5.

Thanks

<html>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

    <script>
    $( document ).ready(function() {
        $("#submit").click(function(e) {
            e.preventDefault();
            var formData = new FormData($(this)[0]);
            $.ajax({
                url: 'http://localhost:8080/api/jsonws/dlapp/add-file-entry?repositoryId=27058&folderId=34530&sourceFileName=Screenshot&mimeType=image/png&title=hello&description=test&changeLog=not',
                type: 'POST',
                data: formData,
                async: false,
                cache: false,
                contentType: false,
                enctype: 'multipart/form-data',
                processData: false,
                success: function (response) {
                    alert(response);
                }
          });
     });
 });
</script>
<form>
    <input type="file" name="file"/>
    <input type="button" value="submit" id="submit"/>
</form>
</html>
Jiten Shahi
  • 41
  • 2
  • 7

1 Answers1

1

Liferay JIRA

You could be encountering LPS-31943. It was a known issue related to this resource action. Please use the patching tool to upgrade to the latest patch version. I see in the comments that users are stating this problem still persists despite the bug being closed in some versions of Chrome.

Error Message

This error message is most commonly seen when you are missing required data from the request. Assuming you are fully patched, it is very likely that your data object is not correct. Even though you are sending the data as query parameters (which could be an issue depending on your JSON WS properties), the data object is still wrong and is therefore giving you the error message.

The HTTP Request

Firstly, Is there a particular reason you are calling the service this way? I believe you can achieve a better result using only HTML

<form action="http://localhost:8080/api/jsonws/dlapp/add-file-entry" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="repositoryId" value="27058"/>
    <input type="hidden" name="folderId" value="34530"/>
    <input type="hidden" name="title" value="hello"/>
    <input type="hidden" name="description" value="test"/>
    <input type="hidden" name="changeLog" value="not"/>
    <input type="file" name="file"/>
    <input type="submit" value="addFileEntry(file)">Submit</input>
</form> 

This code is taken from the official Liferay knowledge base and adapted for your specific example. If you insist on using jQuery you need to stringify your data object. I would do something like this.

<html>
<head></head>
<body>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

    <script>
    $( document ).ready(function() {
        $("#submit").click(function(e) {
            e.preventDefault();
            var formData = JSON.stringify($("form:first").serializeArray());
            $.ajax({
                url: 'http://localhost:8080/api/jsonws/dlapp/add-file-entry',
                type: 'POST',
                data: formData,
                async: false,
                cache: false,
                contentType: false,
                enctype: 'multipart/form-data',
                processData: false,
                success: function (response) {
                    alert(response);
                }
          });
     });
 });
</script>
<form>
        <input type="hidden" name="repositoryId" value="27058"/>
        <input type="hidden" name="folderId" value="34530"/>
        <input type="hidden" name="title" value="hello"/>
        <input type="hidden" name="description" value="test"/>
        <input type="hidden" name="changeLog" value="not"/>
        <input type="file" name="file"/>
        <input type="button" value="submit" id="submit"/>
</form>
</body>
</html>

Best Practice

The sample's above are fine if you are invoking this service from an external application. If, however, you are invoking this from inside the portal (a JavaScript portlet for example) I would definitely utilize Liferay's JavaScript API and the AlloyUI tag libraries.

Liferay.Service(
  '/dlapp/add-file-entry',
  {
    repositoryId: 12345,
    folderId: 2345,
    sourceFileName: 'Screenshoot',
    mimeType: '',
    title: '',
    description: '',
    changeLog: '',
    cmd: {"/dlapp/add-file-entry":{}},
    file: null
  },
  function(obj) {
    console.log(obj);
  }
);

Also if you are building something new you be using Liferay 7 or DXP as it is much more current.

Chris Maggiulli
  • 3,375
  • 26
  • 39