Topic: Form with enctype multipart/form-data not going thru Request Handler
When you use multi-part encoding, you need to access form fields differently from within the servlet/UIM. The
normal HttpServlet.getParameter() method doesn't work, nor does UiModule.getRequestValue(). Consequently,
the "op" parameter is not being recognised, and the request handler is not being called.
The solution is to add the op parameter to the URL:
<form ..... action="%%action%%?op=%%handler%%" ..>
Parameters in the URL are handled in the normal manner, so the op parameter will be recognised. This small change should
completely fix your problem.
For the values of any form fields, you will need to get their value when you iterate through the request parts
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
List<FileItem> items;
try {
items = upload.parseRequest(uh.getRequest());
// Process the uploaded items
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
if (name.equals("description")) {
description = value;
} else if (name.equals("fieldName1")) {
fieldName1 = value;
} else if (name.equals("fieldName2")) {
fieldName2 = value;
} else if (name.equals("fieldName3")) {
fieldName3 = value;
} else if (name.equals("fieldName4")) {
fieldName4 = value;
} else if (name.equals("fieldName5")) {
fieldName5 = value;
} else if (name.equals("fieldName6")) {
fieldName6 = value;
} else if (name.equals("fieldName7")) {
fieldName7 = true;
} else {
// Ignore this
}
} else if ( !isCancelled(items, item.getName())) {
// Get the content of the file
if (item.getName().length() > 0) {
contentType = item.getContentType();
content = item.get();
filename = item.getName();
}
}
}
// Check that required parameters were entered
if (fieldName1 == null)
...
if (fieldName2 == null)
...
etc
} catch (....) {
...
} protected boolean isCancelled(List<FileItem> origLst, String key) {
if (key.equals(""))
return false;
Iterator<FileItem> it = origLst.iterator();
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
if (item.isFormField()) {
if (key.equalsIgnoreCase(item.getString())) {
return true;
}
}
}
return false;
}