Forum

Accueil » Liferay Portal » English » 2. Using Liferay » General

Vue combinée Vue Plate Arborescence
Sujets [ Précédent | Suivant ]
toggle
Sir Seto
Why use serveResource to download a resource... ?
17 juillet 2013 14:38
Réponse

Sir Seto

Rang: New Member

Publications: 2

Date d'inscription: 17 juillet 2013

Publications Récentes

Hi,

I'm new in the portlet's world, and I don't understand something despite a lot of researches...
I know why and how we have to use Ajax with serveResource in portlets, but I don't understand why I should use serveResource to offer a download.

If I want to propose a download (for exemple a PDF file), if the file is into the portlet application, I can make a URL to the file (with getContextPath). If it's an external file, I just can integrate the direct URL (like http://www.website.com/myfile.pdf).

Is it correct ? So what's the interest to use something more complex, with serveResource ? I'm sure I pass next to something important that will explain that...

Thanks for your help !

P.S. : This is the exemple I find to download a resource with serveResource, and the reason that I find not easy to use serveResource to download a resource :
public void serveResource(...)...{
File file = new File(getInitParameter("uploadFolder")
+ File.separator + request.getResourceID());
OutputStream outStream = response.getPortletOutputStream();
if (!file.exists() || !file.canRead()) {
outStream.write("<i>Unable to find the specified file</i>".getBytes());
}
else {
FileInputStream inStream = new FileInputStream(file);
response.setProperty("Content-disposition",
"attachment; filename=\"" +
request.getResourceID() + "\"");
byte[] buffer = new byte[1024];
while (true) {
int bytes = inStream.read(buffer);
if (bytes <= 0) {
break;
}
outStream.write(buffer, 0, bytes);
}
}
outStream.flush();
outStream.close();
}
David H Nebinger
RE: Why use serveResource to download a resource... ?
17 juillet 2013 15:58
Réponse

David H Nebinger

Community Moderator

Rang: Liferay Legend

Publications: 8378

Date d'inscription: 1 septembre 2006

Publications Récentes

Sir Seto:
I'm new in the portlet's world, and I don't understand something despite a lot of researches...
I know why and how we have to use Ajax with serveResource in portlets, but I don't understand why I should use serveResource to offer a download.

If I want to propose a download (for exemple a PDF file), if the file is into the portlet application, I can make a URL to the file (with getContextPath). If it's an external file, I just can integrate the direct URL (like http://www.website.com/myfile.pdf).


Static resources can be handled that way, but dynamic resources cannot.

If you were building the PDF within the code, then you need to get access in the portlet call chain to return it. Action and Render won't serve well because of how they are implemented in the normal portlet event handling.

ServeResource falls outside of that normal event handling and is the focal point for creating dynamic resources to return as downloads. It is also used for AJAX handling because you're just consuming and/or returning data, you're not navigating in the portal.
Sir Seto
RE: Why use serveResource to download a resource... ?
17 juillet 2013 23:57
Réponse

Sir Seto

Rang: New Member

Publications: 2

Date d'inscription: 17 juillet 2013

Publications Récentes

Perfect, thank you ! =)