cancel
Showing results for 
Search instead for 
Did you mean: 

How to access fileupload content from the IPortalComponentRequest object?

Former Member
0 Kudos

I have a form such as the following:

<form
action="/irj/servlet/prt/portal/prtroot/com.my.servlet" 
name=AddFile method=post 
encodingType="multipart/form-data">
<input name=NewFile type=file>
<input type=submit name=AddFile value="Upload">
</form>

now in the servlet, I want to retrieve the file. I'm not using webdynpro, or htmlb or anything like that.

Is there an sap component which could extract the file from the request object? if so, how do I go about it?

I know there is request.getRequestInputStream()

can i extract the file from that?

something like

InputStream filestuff = mumblemumble( request.getRequestInputStream()).mumble("myfile");

IContent filecontent = new com.sapportals.wcm.repository.Content( filestuff, null, -1L);

dircollection.createResource(filename,null,filecontent);

or is there a better way?

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

I think there is no other solution for this problem at this time other than going with the htmlb file upload component.

spicher,

Even though u r on a simple html form you can accomplish this with mix and match of htmlb and hidden variables. Here is the out line.

1) Write a fileupload component using htmlb (you can use jspDyn or abstractportal... doesn't matter)

2) On your html form have a text box and upload button, on click of the upload button, open the fileupload component in a new popup window.

3) on closing this pop up window, set the textbox value with the file name (using javascript opener.document....).

With this you can achieve the file upload .

Former Member
Former Member
0 Kudos

Hello Randall,

I recomend you to use oreilly upload package classes to get your file content. You can get it at http://www.servlets.com/cos/

Regards.

Former Member
0 Kudos

Well, aside from the liscencing restrictions which prevent me from using the oreily classes at our corporation, I don't see how these can help pull things from the SAP framework...

I have been playing with extracting the HttpServletRequest from the IPortalComponentRequest,

(and using com.sap.engine.services.servlets_jsp.lib.multipart) and everything seems fine as long as long as I leave the enctype as application/x-www-form-urlencoded. But as soon as I set it to send multipart/form-data, the portal blows up just loading my servlet.

Former Member
0 Kudos

Hi randall,

You need to use htmlb fileupload control.

Example:

<hbj: fileUpload
      id="myFile">
</hbj:fileUpload>

In your component you will do the following to get the file from request object.

FileUpload file = (FileUpload) getComponentByName("myFile");
String mimetype = file.getFile().getContentType();
String nameoffile = file.getFile().getFileName();
FileInputStream sourceFileInput
try {
 sourceFileInput = new FileInputStream(file.getFile().getFile().getAbsolutePath());
 } catch (Exception e) {
   e.printStackTrace();
}

Former Member
0 Kudos

But the page sending the file does not originate from the portal... it originates from a static html form elsewhere. My servlet is only going to recieve the file, thus I have no htmlb to work with.

and although I found FileUpload in com.sapportals.htmlb.FileUpload, getComponentByName does not resolve.

Former Member
0 Kudos

to make it more clear, i'm working with a portal component.

public class fileproperties extends AbstractPortalComponent {

   public void doContent(
      IPortalComponentRequest request,
      IPortalComponentResponse response) {


   }
}

Former Member
0 Kudos

com.sapportals.portal.prt.util.http.MultiPartParser was able to parse out what I need from the iportalcomponentrequest inputstream.

Former Member
0 Kudos

I accomplished as follows:

[code]

public void doContent(

IPortalComponentRequest request,

IPortalComponentResponse response) {

//// some unrelated stuff

srequest = request.getServletRequest();

contenttype = srequest.getContentType();

String boundary = "";

try {

RE r = new RE("boundary=(.+)$");

r.match(contenttype);

boundary = r.getParen(1);

} catch (Exception e) {

}

MultiPartParser parser = new MultiPartParser( request.getRequestInputStream(), boundary);

try{

//extract parameters

java.util.Hashtable parameters = parser.getParameterParts();

java.util.Enumeration enum = parameters.elements();

while (enum.hasMoreElements()) {

ParamPart pp = (ParamPart) enum.nextElement();

String paramname = pp.getName();

if (paramname.equalsIgnoreCase("myparameter")) {

myparameter = pp.getStringValue();

}

}

} catch (Exception e) {

response.write("\n<p>error in param:" + e.getMessage());

}

try {

//extract files

java.util.Hashtable files = parser.getFileParts();

java.util.Enumeration enum = files.elements();

while (enum.hasMoreElements()) {

FilePart fp = (FilePart) enum.nextElement();

String filepath = fp.getFileName();

String filename="badname";

try {

RE r = new RE("/([^/]+)$");

r.match(filepath.replace('
','/'));

filename = r.getParen(1);

} catch (Exception e) {

}

// dump input stream to a file using other km components

mysavefilefunction(filename, finst);

}

} catch (Exception e){

// handle it

}

}

[/code]

Former Member
0 Kudos

Hi Randall,

where did you find the corresponding jar files?

com.sap.engine.services.servlets_jsp.lib.multipart

Thanks for your input.

Steffen

Former Member
0 Kudos

Hi Antonio,

I had to figure out that with SP14 my implementation of the oreilly package or the jakarta commons is no longer working. I get com.sap.engine.services.httpserver.HttpIOException and cannot access the ServletInputStream...

Do you know more?

Steffen

Former Member
0 Kudos

Ok, ok. Finally I have found the corresponding jar named "sapj2eeclient.jar".

Thanks anyway.

Former Member
0 Kudos

I work by the same metod you use, and I tryed your code. In PDK works ok perfectly but when I upload it to SQP NW the

MultiPartParser returns no file. It seems tahr the request dissapeared:). I tryed apache commons too, and oriley but the same problem apears!

Have any ideea about what 's happening?

Have a good day!

Former Member
0 Kudos

*SQP NW sorry

Former Member
0 Kudos

SAP NW

Former Member
0 Kudos

Hi all,

i too have the same problem and still working on and looking for options. Here are my observations.

1) It won't matter what api you are using to handle the reuest(cos,FileUpload... i tried)

2) This is the problem with both jspDynpage and Ipc if you don't use htmlb components, and want to use plain html. (input type="file"....)

3) looks like the request is stripped off. I placed 2 hidden fields on the form, they are showing to be as null.

If anybody has any idea please let us know.

thax in advance.

bobby

Former Member
0 Kudos

well, this may not be your issue, but are you sure that the form is sending the correct encodingtype? encodingType="multipart/form-data"

Also, i'm on EP6, not sure if NW04 will still work..

<form

action="/irj/servlet/prt/portal/prtroot/com.my.servlet"

name=AddFile method=post

encodingType="multipart/form-data">

<input name=NewFile type=file>

<input type=submit name=AddFile value="Upload">

</form>