on 10-12-2005 5:04 PM
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?
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 .
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Certainly this will help you
http://aluculesa.blogspot.com/2006_09_01_aluculesa_archive.html
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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();
}
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.
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]
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!
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
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>
User | Count |
---|---|
94 | |
11 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.