Hi Frank,
The multiFileUpload uses a collection to store the uploaded files. If you want to have N number of MultiFileUpload then u need N number of collections. So basiclly u need a collection of collection. The first one is to represent each multiFileUpload component and the the second is to hold the uploaded files. Have a look at the sample code.

fBeans is a list of fbean, which store the description of the file, the number of copies, and the collection where the files will be store.



setModel(new CompoundPropertyModel(fBeans));
ListView view = new ListView("fileViewList", fBeans)
           {

               protected void populateItem(ListItem item) {
Fbean f = (Fbean) item.getModelObject();
                   item.add(new Label("desc", f.getDesc()+" : "));
item.add(new MultiFileUploadField("fileInput", new PropertyModel((Fbean) item.getModelObject(), "uploads"), f.getCopies()));
               }
};

Regards,
Al Shamsi

Franklin Antony wrote:
Dear All,
   I am badly in need to implement this. Therefore I am placing the code
here.... I really didnt want to do this. I have a feeling I am placing the
collections in the wrong place.


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.Application;
import org.apache.wicket.Component;
import org.apache.wicket.PageParameters;
import
org.apache.wicket.extensions.ajax.markup.html.form.upload.UploadProgressBar;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.FileUploadField;
import org.apache.wicket.markup.html.form.upload.MultiFileUploadField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.link.PopupSettings;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.file.Files;
import org.apache.wicket.util.file.Folder;
import org.apache.wicket.util.lang.Bytes;
import org.apache.wicket.util.tester.DummyHomePage;

import com.datelservices.domain.Attachment;
import com.datelservices.util.upload.dao.PassportDocDAO;

public class FileUploadPage extends WebPage{
        
public List fileUpload = new ArrayList(); public Collection pageUploads = new ArrayList(); public Collection getPageUploads()
     {
        return pageUploads;
     }
PropertyModel pmPage ;
        

    private class FileUploadForm extends Form
    {


        private  Collection formUploads = new ArrayList();
PropertyModel pmForm =new PropertyModel(this,"formUploads"); public Collection getFormUploads()
        {
                return formUploads;
        }
public FileUploadForm(String name)
        {
            super(name);
setMultiPart(true); List ls = Arrays.asList(new String[]{"Passport ","Scan ","Many
More "});
            //FileListView pl = new FileListView("plist",ls);
ListView pl = new ListView("plist",ls) {
                                protected void populateItem(ListItem arg0) {
                                        
                                        MultiFileUploadField ff=new 
MultiFileUploadField("file_input",new
PropertyModel(FileUploadPage.this,"pageUploads"),3);
                        arg0.add(ff);
                                        arg0.add(new 
Label("lbb",arg0.getModelObjectAsString()));
                                        
                                }
                
            };
pl.setReuseItems(true);
            add(pl);

// Set maximum size double maxLimit =
((UploadApplication)Application.get()).getConfigProperty().getMaxFileUploadSize();
            System.out.println("File upload limit is " + maxLimit);
            setMaxSize(Bytes.kilobytes(maxLimit));
        }

        /**
         * @see org.apache.wicket.markup.html.form.Form#onSubmit()
         */
        protected void onSubmit() {
System.out.println("$$$$$$$$$$$uploads1$$$$$$$$$$$$"+getFormUploads().iterator().hasNext()); //System.out.println("$$$$$$$$$$$uploads2$$$$$$$$$$$$"+((ArrayList)pmPage.getObject()).size()); //System.out.println("$$$$$$$$$$$uploads2.1$$$$$$$$$$$$"+((ArrayList)FileUploadPage.this.pmPage.getObject()).size()); //System.out.println("$$$$$$$$$$$uploads3$$$$$$$$$$$$"+((ArrayList)pmForm.getObject()).size()); System.out.println("$$$$$$$$$$$uploads5$$$$$$$$$$$$"+fileUpload.size()); System.out.println("$$$$$$$$$$$uploads99$$$$$$$$$$$$"+FileUploadForm.this.formUploads.size()); System.out.println("$$$$$$$$$$$uploads99$$$$$$$$$$$$"+FileUploadPage.this.pageUploads.size());
                
                
                
                System.out.println("getting iterator");
                 //Iterator it =
((ArrayList)FileUploadPage.this.pm.getObject()).iterator();
                 Iterator it = fileUpload.iterator();
System.out.println("got iterator");
         }


                
} private int uploadFile(final FileUpload upload , String comments, int
docTypeCD){
if( !upload.getClientFileName().toLowerCase().endsWith(".gif") ){
                FileUploadPage.this.info("Can not upload file : " +
upload.getClientFileName() +". Only GIF file can be uploaded.");
                return -1;
}
        // Create a new file
        PassportDocDAO passportDAO =
((UploadApplication)Application.get()).getPassportDocDAO();
        passportDAO.setComments(comments);
        passportDAO.setDocDate(new Date());
        passportDAO.setDocTypeCD(docTypeCD);
        try {
                        
passportDAO.savePassportDataStream(upload.getInputStream(), (int)
upload.getSize());
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        throw new IllegalStateException("Unable to write file");
                }
                passportDAO.setUserPassportId(1);
                passportDAO.setVersion(0);
                int docId =
((UploadApplication)Application.get()).getIFilePersistService().persistFile(passportDAO);
                FileUploadPage.this.info("saved file: " + 
upload.getClientFileName());

                return docId;
    }


    /** Log. */
    private static final Log log = LogFactory.getLog(FileUploadPage.class);


    /**
     * Constructor.
* * @param parameters
     *            Page parameters
     */
    public FileUploadPage(final PageParameters parameters)
    {
        Folder uploadFolder = getUploadFolder();
// Create feedback panels
        final FeedbackPanel uploadFeedback = new
FeedbackPanel("uploadFeedback");

        // Add uploadFeedback to the page itself
        add(uploadFeedback);

/*        fileListView = new FileListView("fileList", new
LoadableDetachableModel()
        {
            protected Object load()
            {
                return Arrays.asList(getUploadFolder().listFiles());
            }
        });
        add(fileListView);*/
// Add upload form with ajax progress bar
        final FileUploadForm ajaxSimpleUploadForm = new
FileUploadForm("ajax-simpleUpload");
ajaxSimpleUploadForm.add(new UploadProgressBar("progress",
ajaxSimpleUploadForm));
        add(ajaxSimpleUploadForm);
    }
private Folder getUploadFolder()
    {
        return ((UploadApplication)Application.get()).getUploadFolder();
} }


Kindly please advice
Thanks,
Franklin.




Franklin Antony wrote:
Dear Friends,
   I am having a problem with putting MultiFileUploadField inside a
ListView. I am following the example, but somehow the uploads collection
is not getting populated with the files. However when I directly add the
MultiFileUploadField  on a form everything seems to work. Could someone
please shed some light on this.

Here is some code snippet

            List ls = Arrays.asList(new String[]{"Passport "});
            ListView pl = new ListView("plist",ls)
{
                                protected void populateItem(ListItem arg0) {
                                                                                
                                        
                                        MultiFileUploadField ff=new 
MultiFileUploadField("file_input",new
PropertyModel(this,"uploads"),3);
                                        
                                        fileUploadList.add(ff);
                                        fileUpload.add(ff);
                        arg0.add(ff);
        
                                        
        
                                        
                                }
                
                
            };


And the listview is on a form and the collections(uploads) is on the form
to just as in the example. I am sure its something with the component
hierarchy. But not sure how to call the uploads correctly. I think there
is something wrong with the expression.


Thanks,
Franklin



Reply via email to