Forums de discussion

Why use serveResource to download a resource... ?

Sir Seto, modifié il y a 10 années.

Why use serveResource to download a resource... ?

New Member Publications: 2 Date d'inscription: 17/07/13 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();
}
thumbnail
David H Nebinger, modifié il y a 10 années.

RE: Why use serveResource to download a resource... ? (Réponse)

Liferay Legend Publications: 14917 Date d'inscription: 02/09/06 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, modifié il y a 10 années.

RE: Why use serveResource to download a resource... ?

New Member Publications: 2 Date d'inscription: 17/07/13 Publications récentes
Perfect, thank you ! =)