9

I have several contents in my document library (images, documents, videos...) and I want to obtain the download url for each of them programmatically, so I can show them on a jsp in a custom portlet.

Investigating, I have found this page with a possible solution: https://www.liferay.com/community/forums/-/message_boards/message/11073293 but I'm not comfortable with it, since it implies building the url manually with several parameters.

I would like to know if there is a better way ("Liferay way") to do this.

stoldark
  • 454
  • 1
  • 5
  • 15
  • For example, to build a login url you could do it manually with a string '/c/portal/login' and obtaning the p_l_id and concatenating... but instead of doing this manually, you can call themeDisplay.getURLSignIn() and Liferay builds it for you. I'm looking for something similar in this case. – stoldark Aug 13 '12 at 09:38

2 Answers2

8

I am afraid but that is the liferay way :-).

If you still need more confirmation check out source code of the DLUtil#getPreviewURL method.

This method has been used by liferay's Documents & Media portlet and Document and Media display portlet. Also you can check-out the JSP source as to how these portlets use the above method /portal-web/docroot/html/portlet/document_library/action/download.jspf .

P.S.:
You can convert DLFileEntry to FileEntry with this static method.

Prakash K
  • 11,669
  • 6
  • 51
  • 109
  • Thanks Prakash, I am not uncomfortable using the Liferay way, as long as I can use a method like DLUtil.getPreviewURL from my code and abstract from the detail. What I was uncomfortable with, was having to build the url myself manually. – stoldark Aug 13 '12 at 11:07
  • However, I am working with DLFileEntry objects, and DLUtil.getPreviewURL expects a FileEntry object. How do I get around this? I'm trying to cast from DLFileEntry, but that's giving me an error. Any ideas? – stoldark Aug 13 '12 at 11:10
  • 1
    check this static method: http://docs.liferay.com/portal/6.1/javadocs/com/liferay/portlet/documentlibrary/service/DLAppServiceUtil.html#getFileEntry%28long%29 – Prakash K Aug 13 '12 at 11:27
  • 1
    Thanks! That was exactly what I was missing – stoldark Aug 13 '12 at 11:44
  • This is a great solution, I always used to do it similar to how Mark suggests in the other answer by building the url using folder and group IDs! – Jonny Aug 14 '12 at 10:21
  • As of Liferay 7.2.x, DLUtil#getPreviewURL is deprecated and replaced by https://docs.liferay.com/ce/apps/document-library/2.0.0/javadocs/com/liferay/document/library/util/DLURLHelper.html#getPreviewURL-com.liferay.portal.kernel.repository.model.FileEntry-com.liferay.portal.kernel.repository.model.FileVersion-com.liferay.portal.kernel.theme.ThemeDisplay-java.lang.String-boolean-boolean- – Pedro Blandim May 10 '22 at 21:14
6

By such questions I see always to Liferay sources. Here is the fragment that build the file download url in "Documents and Media Library" portlet:

<liferay-ui:input-resource
   url='<%= themeDisplay.getPortalURL() + themeDisplay.getPathContext() + "/documents/" + themeDisplay.getScopeGroupId() + StringPool.SLASH + fileEntry.getUuid() %>'
/>

but you are right, i would prefer a methode like DLFile.getUrl() too.

Mark
  • 17,887
  • 13
  • 66
  • 93