1
votes

I need to save a uploaded file in sub directory of Document & Media folder in liferay from web-form portlet.
I have extended the web Form portlet to do so, but file is getting uploaded successfully in database & not in Document & Media folder.
I tried following code to upload the file in document directory but no success please help .

ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

String title = file.getName();

DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(themeDisplay.getScopeGroupId(), 0, "Test");

ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(),actionRequest);

Map<String, Fields> fieldsMap = new HashMap<String, Fields>();

long fileEntryTypeId = DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT;

FileInputStream inputStream = new FileInputStream(file);

DLFileEntry dlFileEntry = DLFileEntryLocalServiceUtil.addFileEntry(themeDisplay.getUserId(), 10153, dlFolder.getRepositoryId(), 
                            dlFolder.getRepositoryId(), title, file.getContentType(), title, "fileDesc", "sss",
                            fileEntryTypeId, fieldsMap, file, inputStream, file.length(), serviceContext);

inputStream.close();

DLFileEntryLocalServiceUtil.updateFileEntry(themeDisplay.getUserId(), dlFileEntry.getFileEntryId(), title, file.getContentType(),
        title, "fileDesc", "comment", true, dlFileEntry.getFileEntryTypeId(), fieldsMap, file, null, file.length(), serviceContext);
2
This does not even compile - duplicate use of file, I couldn't find this interface of DLFileEntryLocalServiceUtil in docs.liferay.com/portal/6.1/javadocs-all/com/liferay/portlet/…. Please checkOlaf Kock
Hi Olaf Thanks for reply.I am very new in liferay. I have edited my query can you please take a look at it once.. ThanksReeta Wani
What datatype is file here? I have similar problems but at least for java.io.File I don't have a method called getContentType().Joel Peltonen
Also, what does the groupId 10153 refer to? I have no idea what to input there.Joel Peltonen

2 Answers

1
votes

Try this code snippet

ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
ServiceContext serviceContext = ServiceContextFactory.getInstance(actionRequest);

File file = uploadRequest.getFile("file");
String contentType = MimeTypesUtil.getContentType(file);

InputStream inputStream  = new FileInputStream(file);

Folder folderName = DLAppLocalServiceUtil.getFolder(parentRepositoryId, 
                                                    parentFolderId, 
                                                    "Folder Name");
long folderId = folderName.getFolderId();
long repositoryId = folderName.getRepositoryId();

FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry(themeDisplay.getUserId(), 
                                                         repositoryId, 
                                                         folderId, 
                                                         file.getName(), 
                                                         contentType, 
                                                         "File Name", 
                                                         "description", 
                                                         "changeLog", 
                                                         inputStream, 
                                                         file.length(), 
                                                         serviceContext);
0
votes

I know it's an old question, but I had similar issue today. I used DLFileEntryLocalServiceUtil, and I had to call both addFileEntry() and updateFileEntry() in order to correctly create the asset.

See Liferay DLFileEntryLocalServiceUtil.addFileEntry does not create AssetEntry record