Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Matt S.
Upload Image to Document and Media Problem
March 12, 2013 11:22 AM
Answer

Matt S.

Rank: Junior Member

Posts: 36

Join Date: November 29, 2012

Recent Posts

Hi all,

I have a problem to upload an image programmatically, in particular I could create a folder and set the name and description of the file, it appear into my folder but when I go to see the details the picture doesn't appear and the file size is setted to 0.0k . This is my code...

 1
 2
 3ThemeDisplay themeDisplay = (ThemeDisplay) req.getAttribute(WebKeys.THEME_DISPLAY);
 4       UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest( req );
 5      
 6        String tempImageFileName = uploadRequest.getFileName( "image" );
 7        File tempImageFile = uploadRequest.getFile( "image" );
 8        FileEntry tempImageFileEntry = null ;
 9        User utente=PortalUtil.getUser(req);
10        long userId=utente.getUserId();
11        String name=req.getParameter("name");
12        String description=req.getParameter("description");
13       
14             
15                long repositoryId = 10905;
16
17               
18                tempImageFileEntry = DLAppLocalServiceUtil.addFileEntry(
19                    userId ,
20                        repositoryId ,
21                        20702 ,
22                        tempImageFileName,
23                        MimeTypesUtil.getContentType(tempImageFileName) ,
24                        name,
25                        description,
26                        "BRAND NEW",
27                        uploadRequest.getFile( "image" ) ,
28                        ServiceContextFactory.getInstance( DLFileEntry.class.getName(), req) ) ;
29          


my jsp is...

 1
 2<portlet:actionURL name="updateImage" var="uploadImageURL"></portlet:actionURL>
 3
 4
 5<aui:form action="<%= uploadImageURL %>" method="post">
 6<aui:input label="Nome" name="name"></aui:input>
 7<textarea label="Descrizione" name="description"></textarea>
 8<aui:input name="image" type="file"></aui:input>
 9<aui:button-row>
10<aui:button type="submit" value="Carica"></aui:button>
11</aui:button-row>
12</aui:form>


How can I solve it?

Regards
Matt S.
RE: Upload Image to Document and Media Problem
March 12, 2013 3:06 PM
Answer

Matt S.

Rank: Junior Member

Posts: 36

Join Date: November 29, 2012

Recent Posts

Can you help me please?
Amit Doshi
RE: Upload Image to Document and Media Problem
March 13, 2013 12:56 AM
Answer

Amit Doshi

Rank: Liferay Master

Posts: 549

Join Date: December 29, 2010

Recent Posts

Hi Matt,

I am using below code and it is working fine for me.

 1
 2public static FileEntry addFileEntry(PortletRequest portletRequest, PortletResponse portletResponse, Folder parentFolder, File file, String name, String title) {
 3
 4        ThemeDisplay themeDisplay = (ThemeDisplay) portletRequest.getAttribute(WebKeys.THEME_DISPLAY);
 5        HttpServletResponse httpServletResponse = PortalUtil.getHttpServletResponse(portletResponse);
 6
 7        FileEntry fileEntry = null;
 8
 9        try {
10            fileEntry = DLAppLocalServiceUtil.addFileEntry(themeDisplay.getUserId(), parentFolder.getRepositoryId(), parentFolder.getFolderId(), name, MimeTypesUtil.getContentType(file), title, StringPool.BLANK, StringPool.BLANK, file, new ServiceContext());
11
12        } catch (DuplicateFileException e) {
13            httpServletResponse.setStatus(ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION);
14        } catch (FileNameException e) {
15            httpServletResponse.setStatus(ServletResponseConstants.SC_FILE_NAME_EXCEPTION);
16        } catch (FileSizeException e) {
17            httpServletResponse.setStatus(ServletResponseConstants.SC_FILE_SIZE_EXCEPTION);
18        } catch (PortalException e) {
19            LOG.error(e.getMessage(), e);
20        } catch (SystemException e) {
21            LOG.error(e.getMessage(), e);
22        }
23        return fileEntry;
24    }

Please let me know if any concern,

Thanks & Regards,
Amit Doshi
Matt S.
RE: Upload Image to Document and Media Problem
March 13, 2013 3:03 AM
Answer

Matt S.

Rank: Junior Member

Posts: 36

Join Date: November 29, 2012

Recent Posts

Hi Amit and thank you for reply,

I tried but I have the same problem, the strange thing is that i can create the file into document and media but instead of my image appear the default.png image and the size of file is 0 k,I've also tried to dowload this uploaded image but the file results empty so probably I'm wrong when I pass the file from form into jsp to my java class, but I don't understand where. Can you help me please?

Regards