[SOLVED]Download file from Document and Media in custom portlet[SOLVED]Download file from Document and Media in custom portlethttps://liferay.dev/en/c/message_boards/find_thread?p_l_id=119785333&threadId=1182203052024-03-29T14:44:11Z2024-03-29T14:44:11ZRE: Download file from Document and Media in custom portletMassimo Bevilacquahttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1182461622020-01-10T12:34:11Z2020-01-10T12:34:11ZThank you very much, it worksMassimo Bevilacqua2020-01-10T12:34:11ZRE: Download file from Document and Media in custom portletMohammed Yasinhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1182342462020-01-09T10:47:03Z2020-01-09T10:47:03Z<html><head></head><body>Hi,<br>Try adding HttpHeaders.CONTENT_DISPOSITION_ATTACHMENT <br><pre><code>&nbsp;PortletResponseUtil.sendFile(
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;req,
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;res,
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fileEntry.getTitle(),
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inputStream,
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(int) fileEntry.getSize(),&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fileEntry.getMimeType(), HttpHeaders.CONTENT_DISPOSITION_ATTACHMENT);
</code></pre></body></html>Mohammed Yasin2020-01-09T10:47:03Z[SOLVED]Download file from Document and Media in custom portletMassimo Bevilacquahttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1182203042020-01-08T12:20:29Z2020-01-08T12:20:29ZHi everybody,<br />I need to create a link to download a file form "Document and Media" section in my custom portlet.<br />The only problem I have is that instead to download the file, it is open in a new browser tab.<br /><br />I have tried to insert "download" to the <a> tag, but this work only with chrome. <br /><br /> <a class="downloadButton" href="<%=downloadURL%>" download >Download the file</a><br /><br />The downloadURL call the method "serveResource" inside my portlet code. <br /><br /> @Override<br /> public void serveResource(ResourceRequest req, ResourceResponse res){<br /> String fileName = req.getPreferences().getValue("fileName", "");<br /> List<DLFileEntry> ls = DLFileEntryLocalServiceUtil.getDLFileEntries(-1,-1);<br /> fileEntryId = 0L;<br /> for(DLFileEntry ts:ls){<br /> if(ts.getFileName().equalsIgnoreCase(fileName)){ <br /> fileEntryId = ts.getFileEntryId();<br /> System.out.println("File entry: "+ts.getFileName());<br /> }<br /> }<br /> try {<br /> FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(fileEntryId);<br /> InputStream inputStream = DLFileEntryLocalServiceUtil.getFileAsStream(<br /> fileEntry.getPrimaryKey(), fileEntry.getVersion());<br /> PortletResponseUtil.sendFile(<br /> req,<br /> res,<br /> fileEntry.getTitle(),<br /> inputStream,<br /> (int) fileEntry.getSize(), <br /> fileEntry.getMimeType());<br /> } catch (PortalException | IOException e) {<br /> e.printStackTrace();<br /> }<br /> <br /> }<br /> <br /><br />I have also tried to addcom.liferay.portlet.single-page-application=false <br