Foren

Add profile image to liferay user using jsf 2.0

Aryan sds, geändert vor 11 Jahren.

Add profile image to liferay user using jsf 2.0

Junior Member Beiträge: 57 Beitrittsdatum: 24.04.12 Neueste Beiträge
I am trying add profile image to liferay user using jsf portlet with primefaces p:fileUpload component.

Now as we know to add profile image to user we use

  UserLocalServiceUtil.updatePortrait(user, bytes_file); 




where user is userid and bytes_file is array of bytes

to get value of bytes_file liferay use this way.


 UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
    
    File file=uploadRequest.getFile(fileName);
    
    i.e byte[] bytes=FileUtil.getBytes(file);


But as I am using jsf portlet , unable to get actionrequest object, so how can I get actionrequest object ?

I have also tried to get actionrequest using this ways.
    
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    ActionRequest actionRequest = (ActionRequest) externalContext.getRequest();


or

 LiferayFacesContext liferayFacesContext = LiferayFacesContext.getInstance();
    ActionRequest actionRequest = (ActionRequest) liferayFacesContext.getPortletRequest();


but it's giving me errors as com.liferay.portlet.ResourceRequestImpl
cannot be cast to javax.portlet.ActionRequest


I have also tried in this way ....

    step 1.  byte[] bytes_file = event.getFile().getFileName().getBytes();
    step 2.   UserLocalServiceUtil.updatePortrait(user, by_new);


but it's giving me exception as com.liferay.portal.UserPortraitTypeException which means that we get this exception when

   renderedImage is null as shown below.
    ImageBag imageBag = ImageToolUtil.read(bytes);
    
    			RenderedImage renderedImage = imageBag.getRenderedImage();
    
    			if (renderedImage == null) {
    				throw new UserPortraitTypeException();
    			}
    
    	




so how can I get correct actionrequest to use liferay's way to add
image to a user ? or how can i pass correct bytes value of upload
image file to add image to user?




Or is there any other way to add user profile image in liferay ?
thumbnail
Juan Gonzalez, geändert vor 11 Jahren.

RE: Add profile image to liferay user using jsf 2.0

Liferay Legend Beiträge: 3089 Beitrittsdatum: 28.10.08 Neueste Beiträge
Probably you'll want to see and download jsf2 portlet demo:

https://github.com/liferay/liferay-faces/blob/3.2.x/demos/bridge/jsf2-portlet/src/main/java/com/liferay/faces/demos/bean/ApplicantBackingBean.java
Aryan sds, geändert vor 11 Jahren.

RE: Add profile image to liferay user using jsf 2.0

Junior Member Beiträge: 57 Beitrittsdatum: 24.04.12 Neueste Beiträge
Juan Gonzalez:
Probably you'll want to see and download jsf2 portlet demo:

https://github.com/liferay/liferay-faces/blob/3.2.x/demos/bridge/jsf2-portlet/src/main/java/com/liferay/faces/demos/bean/ApplicantBackingBean.java



Thank you for quick reply Juan.

I have gone through ApplicantBackingBean.java file having handleFileUpload(FileUploadEvent fileUploadEvent) method, but in this method they are not using any actionrequest object or not shown way how to get correct bytes from file object to which we will pass uploading file-name.

I think that's why I am getting exception as com.liferay.portal.UserPortraitTypeException .

So how can I overcome from this exception as it occurs when renderImage is null as shown below.

ImageBag imageBag = ImageToolUtil.read(bytes);

            RenderedImage renderedImage = imageBag.getRenderedImage();

            if (renderedImage == null) {
                throw new UserPortraitTypeException();
            }.
thumbnail
Juan Gonzalez, geändert vor 11 Jahren.

RE: Add profile image to liferay user using jsf 2.0

Liferay Legend Beiträge: 3089 Beitrittsdatum: 28.10.08 Neueste Beiträge
You have to read code carefully.

There you can access UploadedFile class, which has all of these methods:


https://github.com/liferay/liferay-faces/blob/3.2.x/bridge-impl/src/main/java/com/liferay/faces/bridge/model/UploadedFile.java

You can then access the file bytes.
thumbnail
Neil Griffin, geändert vor 11 Jahren.

RE: Add profile image to liferay user using jsf 2.0

Liferay Legend Beiträge: 2655 Beitrittsdatum: 27.07.05 Neueste Beiträge
I would also recommend that you look at the source code for the all new primefaces3-users-portlet because it has the ability to upload a user portrait for the selected user. It has a nice feature in which it uses a JSF2 ResourceHandler to provide a URL to the image after it has been uploaded. For now you will have to build it from source, but the binary WAR will be available when Liferay Faces 3.1.2-ga3 is released.