Save file to Document directory in liferay 6.1 using API

前端 未结 2 990
Happy的楠姐
Happy的楠姐 2021-01-03 17:31

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

相关标签:
2条回答
  • 2021-01-03 17:42

    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

    0 讨论(0)
  • 2021-01-03 17:47

    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 讨论(0)
提交回复
热议问题