Skip to Content
author's profile photo Former Member
Former Member

FileUpload problem

Hello all,

I'm using the FileUpload component in a JSPDynPage but it does not seem to work for me. The problem is a lot like the one Patrick O'Neill asked about. I used the HTMLB example but for some strange reason variable fu remains NULL. Please have a look at my code and explain to me what the problem is. The JSP contains a file upload component and a button to start the upload event.

This is the java code I used (testprogram):

import com.sapportals.htmlb.enum.ButtonDesign;

import com.sapportals.htmlb.event.Event;

import com.sapportals.htmlb.page.DynPage;

import com.sapportals.htmlb.page.PageException;

import com.sapportals.portal.htmlb.page.JSPDynPage;

import com.sapportals.portal.htmlb.page.PageProcessorComponent;

import com.sapportals.portal.prt.component.IPortalComponentContext;

import com.sapportals.portal.prt.component.IPortalComponentProfile;

import com.sapportals.portal.prt.component.IPortalComponentRequest;

// for htmlb upload

import java.io.File;

import com.sapportals.htmlb.rendering.IFileParam;

import com.sapportals.htmlb.FileUpload;

// for debugging

import com.sapportals.portal.prt.runtime.PortalRuntime;

import com.sapportals.portal.prt.logger.ILogger;

import com.sapportals.portal.prt.logger.Level;

import bean.FileUploadBean;

public class FileUploadExample extends PageProcessorComponent {

public DynPage getPage() {

return new RealJSPDynPage();

}

public class RealJSPDynPage extends JSPDynPage {

FileUploadBean myBean;

private ILogger defaultLogger = PortalRuntime.getLogger();

public RealJSPDynPage() {

}

public void doInitialization() {

// Get the request, context and profile from portal platform

IPortalComponentRequest request = (IPortalComponentRequest) this.getRequest();

IPortalComponentContext myContext = request.getComponentContext();

IPortalComponentProfile myProfile = myContext.getProfile();

defaultLogger.setActive(true);

defaultLogger.setLevel(Level.ALL);

// Initialization of bean

FileUploadBean myBean = new FileUploadBean();

setBeanFromProfile(myProfile, myBean);

// Put the bean into the application context

myContext.putValue("myBeanName", myBean);

}

public void doProcessAfterInput() throws PageException {

// Get the request, context and profile from portal platform

IPortalComponentRequest request = (IPortalComponentRequest) this.getRequest();

IPortalComponentContext myContext = request.getComponentContext();

IPortalComponentProfile myProfile = myContext.getProfile();

// Get the bean from application context

myBean = (FileUploadBean) myContext.getValue("myBeanName");

setBeanFromProfile(myProfile, myBean);

}

public void doProcessBeforeOutput() throws PageException {

setJspName("fileupload.jsp");

}

public void onClick(Event event) throws PageException {

myBean.setText("I was clicked");

String ivSelectedFileName;

FileUpload fu = (FileUpload) this.getComponentByName("myfileupload");

// debug info message, other option is: severe

defaultLogger.info(this, "Hello this is a test");

if (fu == null) {

// error

defaultLogger.severe(this, "strange error");

}

// this is the temporary file

if (fu != null) {

// Output to the console to see size and UI.

System.out.println(fu.getSize());

System.out.println(fu.getUI());

defaultLogger.info(this, String.valueOf(fu.getSize()));

defaultLogger.info(this, fu.getUI());

// Get file parameters and write it to the console

IFileParam fileParam = fu.getFile();

System.out.println(fileParam);

// Get the temporary file name

File f = fileParam.getFile();

String fileName = fileParam.getFileName();

defaultLogger.info(this, fileName);

// Get the selected file name and write ti to the console

ivSelectedFileName = fu.getFile().getSelectedFileName();

System.out.println("selected filename: "+ivSelectedFileName);

defaultLogger.info(this, "selected filename: "+ivSelectedFileName);

}

}

private void setBeanFromProfile(IPortalComponentProfile myProfile, FileUploadBean myBean) {

// FileUpload properties

myBean.setSize(myProfile.getProperty("size"));

myBean.setMaxLength(myProfile.getProperty("maxLength"));

myBean.setAccept(myProfile.getProperty("accept"));

// Button properties

myBean.setDisabled(myProfile.getProperty("disabled"));

myBean.setDesign(ButtonDesign.getEnumByString(myProfile.getProperty("design")));

myBean.setEncode(myProfile.getProperty("encode"));

myBean.setText(myProfile.getProperty("text"));

myBean.setTooltip(myProfile.getProperty("tooltip"));

myBean.setWidth(myProfile.getProperty("width"));

}

}

}

This is the JSP:

<%--- FileUpload.jsp --%>

<%@ taglib uri= "tagLib" prefix="hbj" %>

<%--- Get the Bean named myBeanName from the application context --%>

<jsp:useBean id="myBeanName" scope="application" class="bean.FileUploadBean" />

<hbj:content id="myContext" >

<hbj:page title="Template for a portal component">

<hbj:form id="myFormId" encodingType="multipart/form-data">

<hbj:fileUpload

id="myFileUpload"

size="<%=myBeanName.getSize() %>"

maxLength="<%=myBeanName.getMaxLength() %>"

accept="<%=myBeanName.getAccept()%>">

</hbj:fileUpload>

<hbj:button

id="MyButton"

text="<%=myBeanName.getText() %>"

width="<%=myBeanName.getWidth() %>"

tooltip="<%=myBeanName.getTooltip() %>"

onClick="click"

design="<%=myBeanName.getDesign().getStringValue() %>"

disabled="<%=myBeanName.isDisabled() %>"

encode="<%=myBeanName.isEncode() %>">

</hbj:button>

</hbj:form>

</hbj:page>

</hbj:content>

The Bean:

package bean;

import com.sapportals.htmlb.enum.ButtonDesign;

public class FileUploadBean

{

// FileUpload

public String size;

public String maxLength;

public String accept;

public String getSize() {

return size;

}

public void setSize(String size) {

this.size = size;

}

public String getMaxLength() {

return maxLength;

}

public void setMaxLength(String maxLength) {

this.maxLength = maxLength;

}

public String getAccept() {

return accept;

}

public void setAccept(String accept) {

this.accept = accept;

}

// Button

public String disabled;

public ButtonDesign design;

public String encode;

public String text;

public String tooltip;

public String width;

// constructor

// public ButtonBean() {

// }

public String isDisabled() {

return disabled;

}

public void setDisabled(String disabled) {

this.disabled = disabled;

}

public ButtonDesign getDesign() {

return design;

}

public void setDesign(ButtonDesign design) {

this.design = design;

}

public String isEncode() {

return encode;

}

public void setEncode(String encode) {

this.encode = encode;

}

public String getText() {

return text;

}

public void setText(String text) {

this.text = text;

}

public String getTooltip() {

return tooltip;

}

public void setTooltip(String tooltip) {

this.tooltip = tooltip;

}

public String getWidth() {

return width;

}

public void setWidth(String width) {

this.width = width;

}

}

and the Portalapp.xml

<?xml version="1.0" encoding="utf-8"?>

<application>

<application-config>

<property name="startup" value="true"/>

<property name="SharingReference" value="htmlb"/>

</application-config>

<components>

<component name="FileUpload">

<component-config>

<property name="ClassName" value="FileUploadExample"/>

<property name="SecurityZone" value="com.sap.portal.pdk/low_safety"/>

</component-config>

<component-profile>

<property name="maxLength" value="125000">

<property name="personalization" value="dialog"/>

</property>

<property name="accept" value="Accept">

<property name="personalization" value="dialog"/>

</property>

<property name="size" value="50">

<property name="personalization" value="dialog"/>

</property>

<property name="width" value="250">

<property name="personalization" value="dialog"/>

<property name="type" value="number"/>

</property>

<property name="disabled" value="FALSE">

<property name="personalization" value="dialog"/>

<property name="type" value="boolean"/>

</property>

<property name="encode" value="True">

<property name="personalization" value="dialog"/>

<property name="type" value="boolean"/>

</property>

<property name="tooltip" value="Initial Tooltip">

<property name="personalization" value="dialog"/>

</property>

<property name="text" value="Initial Button Text">

<property name="personalization" value="dialog"/>

</property>

<property name="tagLib" value="/SERVICE/htmlb/taglib/htmlb.tld"/>

<property name="design" value="STANDARD">

<property name="personalization" value="dialog"/>

<property name="type" value="select[STANDARD,SMALL,EMPHASIZED]"/>

</property>

</component-profile>

</component>

</components>

<services/>

</application>

Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • Best Answer
    Posted on Jan 04, 2005 at 01:27 PM

    Hi Raymond,

    within your JSP, you have given the ID "myFileUpload" whereas within your coding you try to retrieve a component with ID "myfileupload". That's all so far.

    Some additinal hints:

    When referring to another thread, please give the link; in this case, you talked about Problem with FileUpload component

    If you give (non) working examples, please reduce them to an absolute minimum, so that they show what they should show, but nothing more. This does not only hold on SDN but for all bug tracking systems. People get ill from reading 1000 lines of code where 10 would do 😊

    And for formatting on SDN, you can use [ code ] and [/ code ] (without spaces), makes life easier too.

    And don't forget to press the yellow star button on helping answers... 😊

    So far for today 😊

    Hope it helps

    Detlev

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.