Expand my Community achievements bar.

SOLVED

File upload in jQuery using the custom SlingPOSTServlet

Avatar

Former Community Member

I'm trying to upload a file to my crx repository in Jquery. However the script runs in a separate server than the which hosts CQ. Because of this the request is a CORS one. Also custom header are being set while the request is being sent because of which a preFlight doOptions is being sent. This sends backs a 403 forbidden and the POST doesn't take place. I tried posing the file using the chrome extension POSTMAN which worked.

The source of SlingPostServlet - http://grepcode.com/file/repo1.maven.org/maven2/org.apache.sling/org.apache.sling.servlets.post/2.3.... and it obviously will not send doesn't send back the custom headers back in it's response. I tried extending the SlingPostServlet and creating a servlet, however on deploying that, the bundle was in installed state.The osgi bundle org.apache.sling.servlets.post doesn't seem to export the package org.apache.sling.servlets.post.impl because of which the bundle is in installed state.

I could always create another servlet and achieve this which is mentioned here - http://helpx.adobe.com/experience-manager/using/uploading-files-aem1.html, but is there any workaround for this so that i can persist data directly to /content/foo/bar... using the restful way

Thanks.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Uploading the file as mentioned in that article is a restful way. You are using AJAX to make a request to a servlet.  (http://rest.elkstein.org/2008/02/ajax-and-rest.html)

The only difference is you are posting to an AEM custom Sling Servlet (not the out of the box one) - which is better IMHO because you have full control over what happens once the file hits the back end servlet. You can process the file, (maybe add a log to meet your business requirements), and then add it to the JCR. 

View solution in original post

1 Reply

Avatar

Correct answer by
Level 10

Uploading the file as mentioned in that article is a restful way. You are using AJAX to make a request to a servlet.  (http://rest.elkstein.org/2008/02/ajax-and-rest.html)

The only difference is you are posting to an AEM custom Sling Servlet (not the out of the box one) - which is better IMHO because you have full control over what happens once the file hits the back end servlet. You can process the file, (maybe add a log to meet your business requirements), and then add it to the JCR.