Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically downloading image from CQ5

Tags:

aem

I am trying to programmatically download a image from CQ5.I have made a link which when clicked should download a image.To do this I have made a ajax call to a servlet ,for whenever user clicks on the image,we should get a pop of open and save dialog. This is the code I have written in servlet to download the image.

**response.setContentType("image/png");
response.setHeader("Content-Disposition", "attachment; filename=icon" + ".png");
URL url = new URL("http://somehost:portnmuber/content/dam/image.jpg");
URLConnection connection = url.openConnection();
InputStream stream = connection.getInputStream();**

I am continuously getting 401 error. Is there some another way that I can do this.

Thanks,in advance.

like image 295
Param1414086 Avatar asked Dec 10 '25 05:12

Param1414086


1 Answers

Firstly, have a look at the download component in the foundation libs: /libs/foundation/components/download/download.jsp

Secondly, if you don't have a session open with CQ5, it will by default give you a 401 error. I am assuming you are in a generated container for CQ5, or are you just randomly accessing from another domain?

Note that even for anonymous access, CQ will still establish an anonymous session, and give you a login token. Anonymous is effectively still authenticated, just without the requirement of a username and password.

like image 180
Bayani Portier Avatar answered Dec 12 '25 01:12

Bayani Portier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!