RE: Liferay 7.2 - How to store images in my Database

Fabio Carvalho, modified 6 Years ago. Junior Member Posts: 81 Join Date: 6/25/19 Recent Posts
Hi,

I want to add a feature to my website, but I am not sure what would be the best approach for this.

I have created a portlet to add a Product Object to my database through a Form. The submit button calls an ActionURL and in my Java class I use the Service Builder to add the new Product to my database. This is working perfect! But I also would like to add images to this product creation. So my idea would be to have a Div on my portlet that let's you upload multiple images with a preview and after the upload register the location of those images on my database to show them in the future.

So, my questions are:
Does Liferay have a dedicated folder where I could store my files uploaded? And how should I use them?
I don't think that Liferay has a native multiple image upload widget, so what would be the best approach to add one of these?
thumbnail
Mohammed Yasin, modified 6 Years ago. Liferay Master Posts: 593 Join Date: 8/8/14 Recent Posts
Hi , 
For uploading the document and saving it in Liferay DMS you can refer below referencehttp://liferayiseasy.blogspot.com/2015/07/folder-and-file-upload-programmatically.html.
For doing multiple uploads you can do it using liferay-upload  script  You can refer below  code and url
https://github.com/liferay/liferay-portal/blob/7.2.x/modules/apps/document-library/document-library-web/src/main/resources/META-INF/resources/document_library/upload_multiple_file_entries.jsp
​​​​​​​
<aui:form name="fm1"></aui:form>
<div class="lfr-upload-container" id="<portlet:namespace />fileUpload"></div>
<aui:script use="liferay-upload"></aui:script>
new Liferay.Upload(
{
boundingBox: '#<portlet:namespace />fileUpload',
&lt;%
DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(locale);
%&gt;
decimalSeparator: '&lt;%= decimalFormatSymbols.getDecimalSeparator() %&gt;',
deleteFile: '<liferay-portlet:actionurl name="/document_library/upload_multiple_file_entries"><portlet:param name="<%= Constants.CMD %>" value="<%= Constants.DELETE_TEMP %>" /><portlet:param name="folderId" value="<%= String.valueOf(folderId) %>" /></liferay-portlet:actionurl>',
fileDescription: '&lt;%= StringUtil.merge(dlConfiguration.fileExtensions()) %&gt;',
maxFileSize: '&lt;%= dlConfiguration.fileMaxSize() %&gt; B',
metadataContainer: '#<portlet:namespace />commonFileMetadataContainer',
metadataExplanationContainer: '#<portlet:namespace />metadataExplanationContainer',
namespace: '<portlet:namespace />',
tempFileURL: {
method: Liferay.Service.bind('/dlapp/get-temp-file-names'),
params: {
folderId: &lt;%= folderId %&gt;,
folderName: '&lt;%= EditFileEntryMVCActionCommand.TEMP_FOLDER_NAME %&gt;',
groupId: &lt;%= scopeGroupId %&gt;
}
},
tempRandomSuffix: '&lt;%= TempFileEntryUtil.TEMP_RANDOM_SUFFIX %&gt;',
uploadFile: '<liferay-portlet:actionurl name="/document_library/upload_multiple_file_entries"><portlet:param name="<%= Constants.CMD %>" value="<%= Constants.ADD_TEMP %>" /><portlet:param name="folderId" value="<%= String.valueOf(folderId) %>" /></liferay-portlet:actionurl>'
}
);
Fabio Carvalho, modified 6 Years ago. Junior Member Posts: 81 Join Date: 6/25/19 Recent Posts
Hi Mohammed,

I was following your first link to create a documents and media folder and ended up with this:
public void addProduct(ActionRequest request, ActionResponse response) throws PortalException, FileNotFoundException {
&nbsp; &nbsp; ThemeDisplay theme = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
&nbsp; &nbsp; if (!folderExists(theme)) addFolder(request, theme);
}

private boolean folderExists(ThemeDisplay theme) {
&nbsp; &nbsp; if (DLFolderLocalServiceUtil.fetchFolder(theme.getScopeGroupId(), DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, "Product Images") == null) return false;
&nbsp; &nbsp; else return true;
}
​​​​​​​
private void addFolder(ActionRequest request, ThemeDisplay theme) throws PortalException &nbsp;{
&nbsp; &nbsp; long userId = theme.getUserId();
&nbsp; &nbsp; long groupId = theme.getScopeGroupId();
&nbsp; &nbsp; long repositoryId = theme.getScopeGroupId();
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp; &nbsp; long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
&nbsp; &nbsp; String folderName = "Product Images";
&nbsp; &nbsp; String folderDescription = "Folder to store product images";
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp; &nbsp; ServiceContext context = ServiceContextFactory.getInstance(DLFolder.class.getName(), request);
&nbsp; &nbsp; DLFolderLocalServiceUtil.addFolder(userId, groupId, repositoryId, false, parentFolderId, folderName, null, false, context);
}

This is creating the folder perfectly. The problem is: in my portal if I go to Content & Data -> Documents and Media -> Select the created folder and try to move it to the recycle bin, my request fails to complete. This doesn't happens if I create a folder on my portal instead of creating it programmatically. Any clue why this is happening? Am I passing a wrong param?
thumbnail
Mohammed Yasin, modified 6 Years ago. Liferay Master Posts: 593 Join Date: 8/8/14 Recent Posts
Hi, Can you please share the error log your getting when deleting the folder.I would suggest you to use   below api for adding folder
&nbsp;DLAppLocalServiceUtil.addFolder(userId, repositoryId, parentFolderId, name, description, serviceContext)&nbsp;
Fabio Carvalho, modified 6 Years ago. Junior Member Posts: 81 Join Date: 6/25/19 Recent Posts
Hi,

Looks like by using the DLAppLocalServiceUtil I am able to delete the created folder. Thanks a lot!