Hi,

I've managed to create a workarounf for this a while ago.
I thought about adding it to StripesStuff project, but it would be better if it 
was in Stripes itself.
I think it would be easy to change it so that we won't need to add a "[0]" at 
the end of the name element.

In the ActionBean:
    @Validate
    public List<FileBean> files;

In the JSP:
<stripes-dynamic:file id="files" name="files[0]" multiple="multiple"/>

You need a new 
net.sourceforge.stripes.controller.multipart.CommonsMultipartWrapper.
    @SuppressWarnings("unchecked")
    public void build(HttpServletRequest request, File tempDir, long 
maxPostSize)
            throws IOException, FileUploadLimitExceededException {
        String charset;
        Map<String,FileItem> files = new HashMap<String,FileItem>();
        Map<String,String[]> parameters = new HashMap<String, String[]>();
        
        try {
            charset = request.getCharacterEncoding();
            DiskFileItemFactory factory = new DiskFileItemFactory();
            factory.setRepository(tempDir);
            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setSizeMax(maxPostSize);
            
            List<FileItem> items = upload.parseRequest(request);
            Map<String,List<String>> params = new HashMap<String, 
List<String>>();

            for (FileItem item : items) {
                // If it's a form field, add the string value to the list
                if (item.isFormField()) {
                    List<String> values = params.get(item.getFieldName());
                    if (values == null) {
                        values = new ArrayList<String>();
                        params.put(item.getFieldName(), values);
                    }
                    values.add(charset == null ? item.getString() : 
item.getString(charset));
                }
                // Else store the file param
                else {
                    processFile(item, files);
                }
            }

            // Now convert them down into the usual map of String->String[]
            for (Map.Entry<String,List<String>> entry : params.entrySet()) {
                List<String> values = entry.getValue();
                parameters.put(entry.getKey(), values.toArray(new 
String[values.size()]));
            }
        }
        catch (FileUploadBase.SizeLimitExceededException slee) {
            throw new FileUploadLimitExceededException(maxPostSize, 
slee.getActualSize());
        }
        catch (FileUploadException fue) {
            IOException ioe = new IOException("Could not parse and cache file 
upload data.");
            ioe.initCause(fue);
            throw ioe;
        }

        try {
            Field charsetField = 
net.sourceforge.stripes.controller.multipart.CommonsMultipartWrapper.class.getDeclaredField("charset");
            if (!charsetField.isAccessible()) {
                charsetField.setAccessible(true);
            }
            charsetField.set(this, charset);
            Field filesField = 
net.sourceforge.stripes.controller.multipart.CommonsMultipartWrapper.class.getDeclaredField("files");
            if (!filesField.isAccessible()) {
                filesField.setAccessible(true);
            }
            filesField.set(this, files);
            Field parametersField = 
net.sourceforge.stripes.controller.multipart.CommonsMultipartWrapper.class.getDeclaredField("parameters");
            if (!parametersField.isAccessible()) {
                parametersField.setAccessible(true);
            }
            parametersField.set(this, parameters);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException("Could not set fields in super class", 
e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException("Could not set fields in super class", 
e);
        }
    }
    private void processFile(FileItem item, Map<String,FileItem> files) {
        if (item.getFieldName().endsWith("[0]")
                && files.containsKey(item.getFieldName())) {
            String baseName = item.getFieldName().substring(0, 
item.getFieldName().length() - 3);
            int index = 0;
            while (files.containsKey(baseName + "[" + index + "]")) {
                index++;
            }
            log.trace("renaming file parameter " + item.getFieldName() + " to " 
+ baseName + "[" + index + "]");
            files.put(baseName + "[" + index + "]", item);
        } else {
            files.put(item.getFieldName(), item);
        }
    }

Christian

-----Message d'origine-----
De : Mike McNally [mailto:[email protected]] 
Envoyé : January-12-12 12:25 PM
À : Stripes Users List
Objet : [Stripes-users] File inputs with the new "multiple" attribute

In newer browsers, it's possible to give input elements of type "file"
the "multiple" attribute. That tells the browser to allow more than
one file to be selected in the file chooser. That, in turn, results in
a multi-valued request parameter.

When I try this, however, it doesn't work, a situation which I think
is documented on the wiki.  However that documentation was probably
written before the advent of the "multiple" attribute, so the
painfulness of the situation was not as evident then (and thus the
indexed parameter name solution seemed quite reasonable). There's no
way to fix this however; the browser is completely in charge of
preparing the POST data.

My question is, what is it that would have to be fixed in order to
support "multiple" file parameters?  The web is moving pretty fast
nowadays :-)


-- 
Turtle, turtle, on the ground,
Pink and shiny, turn around.

------------------------------------------------------------------------------
RSA(R) Conference 2012
Mar 27 - Feb 2
Save $400 by Jan. 27
Register now!
http://p.sf.net/sfu/rsa-sfdev2dev2
_______________________________________________
Stripes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-users

------------------------------------------------------------------------------
RSA(R) Conference 2012
Mar 27 - Feb 2
Save $400 by Jan. 27
Register now!
http://p.sf.net/sfu/rsa-sfdev2dev2
_______________________________________________
Stripes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-users

Reply via email to