From dd4be4b6b6d25319c28c30554242fefa5a09c190 Mon Sep 17 00:00:00 2001 From: hns Date: Mon, 15 Mar 2004 12:10:26 +0000 Subject: [PATCH] Make form parsing aware of multiple values per key for file upload forms (enctype="multipart/form-data") --- src/helma/servlet/AbstractServletClient.java | 7 +++++- src/helma/util/FileUpload.java | 23 ++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/helma/servlet/AbstractServletClient.java b/src/helma/servlet/AbstractServletClient.java index 3463bc90..45d57386 100644 --- a/src/helma/servlet/AbstractServletClient.java +++ b/src/helma/servlet/AbstractServletClient.java @@ -165,7 +165,12 @@ public abstract class AbstractServletClient extends HttpServlet { String nextKey = (String) e.nextElement(); Object nextPart = parts.get(nextKey); - reqtrans.set(nextKey, nextPart); + if (nextPart instanceof List) { + reqtrans.set(nextKey, ((List) nextPart).get(0)); + reqtrans.set(nextKey+"_array", ((List) nextPart).toArray()); + } else { + reqtrans.set(nextKey, nextPart); + } } } } catch (Exception upx) { diff --git a/src/helma/util/FileUpload.java b/src/helma/util/FileUpload.java index 4baae320..88f6b14c 100644 --- a/src/helma/util/FileUpload.java +++ b/src/helma/util/FileUpload.java @@ -124,12 +124,27 @@ public class FileUpload { } } - if (filename != null) { - MimePart part = new MimePart(filename, newb, type); + Object existingValue = parts.get(name); + Object newValue = null; - parts.put(name, part); + if (filename != null) { + newValue = new MimePart(filename, newb, type); } else { - parts.put(name, new String(newb, encoding)); + newValue = new String(newb, encoding); + } + + if (existingValue == null) { + // no previous value, just add new object + parts.put(name, newValue); + } else if (existingValue instanceof ArrayList) { + // already multiple values, add to list + ((ArrayList) existingValue).add(newValue); + } else { + // already one value, convert to list + ArrayList list = new ArrayList(); + list.add(existingValue); + list.add(newValue); + parts.put(name, list); } } }