Search in sources :

Example 1 with IMultipartWebRequest

use of org.apache.wicket.protocol.http.IMultipartWebRequest in project wicket by apache.

the class FileUploadField method getFileUploads.

/**
 * @return a list of all uploaded files. The list is empty if no files were selected. It will return more than one files if:
 *         <ul>
 *         <li>HTML5 &lt;input type="file" <strong>multiple</strong> /&gt; is used</li>
 *         <li>the browser supports <em>multiple</em> attribute</li>
 *         <li>the user has selected more than one files from the <em>Select file</em> dialog</li>
 *         </ul>
 */
public List<FileUpload> getFileUploads() {
    if (fileUploads != null) {
        return fileUploads;
    }
    fileUploads = new ArrayList<>();
    // Get request
    final Request request = getRequest();
    // If we successfully installed a multipart request
    if (request instanceof IMultipartWebRequest) {
        // Get the item for the path
        final List<FileItem> fileItems = ((IMultipartWebRequest) request).getFile(getInputName());
        if (fileItems != null) {
            for (FileItem item : fileItems) {
                // WICKET-6270 detect empty field by missing file name
                if (Strings.isEmpty(item.getName()) == false) {
                    fileUploads.add(new FileUpload(item));
                }
            }
        }
    }
    return fileUploads;
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) IMultipartWebRequest(org.apache.wicket.protocol.http.IMultipartWebRequest) Request(org.apache.wicket.request.Request) IMultipartWebRequest(org.apache.wicket.protocol.http.IMultipartWebRequest)

Example 2 with IMultipartWebRequest

use of org.apache.wicket.protocol.http.IMultipartWebRequest in project wicket by apache.

the class MultiFileUploadField method getInputAsArray.

/**
 * @see org.apache.wicket.markup.html.form.FormComponent#getInputAsArray()
 */
@Override
public String[] getInputAsArray() {
    if (inputArrayCache == null) {
        // this array will aggregate all input names
        ArrayList<String> names = null;
        final Request request = getRequest();
        if (request instanceof IMultipartWebRequest) {
            // retrieve the filename->FileItem map from request
            final Map<String, List<FileItem>> itemNameToItem = ((IMultipartWebRequest) request).getFiles();
            for (Entry<String, List<FileItem>> entry : itemNameToItem.entrySet()) {
                // iterate over the map and build the list of filenames
                final String name = entry.getKey();
                final List<FileItem> fileItems = entry.getValue();
                if (!Strings.isEmpty(name) && name.startsWith(getInputName() + MAGIC_SEPARATOR) && !fileItems.isEmpty() && !Strings.isEmpty(fileItems.get(0).getName())) {
                    // make sure the fileitem belongs to this component and
                    // is not empty
                    names = (names != null) ? names : new ArrayList<String>();
                    names.add(name);
                }
            }
        }
        if (names != null) {
            inputArrayCache = names.toArray(new String[names.size()]);
        }
    }
    return inputArrayCache;
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) IMultipartWebRequest(org.apache.wicket.protocol.http.IMultipartWebRequest) Request(org.apache.wicket.request.Request) IMultipartWebRequest(org.apache.wicket.protocol.http.IMultipartWebRequest) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with IMultipartWebRequest

use of org.apache.wicket.protocol.http.IMultipartWebRequest in project wicket by apache.

the class MultiFileUploadField method convertValue.

@Override
protected Collection<FileUpload> convertValue(String[] value) throws ConversionException {
    // convert the array of filenames into a collection of FileItems
    Collection<FileUpload> uploads = null;
    final String[] filenames = getInputAsArray();
    if (filenames != null) {
        final IMultipartWebRequest request = (IMultipartWebRequest) getRequest();
        uploads = new ArrayList<>(filenames.length);
        for (String filename : filenames) {
            List<FileItem> fileItems = request.getFile(filename);
            for (FileItem fileItem : fileItems) {
                uploads.add(new FileUpload(fileItem));
            }
        }
    }
    return uploads;
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) IMultipartWebRequest(org.apache.wicket.protocol.http.IMultipartWebRequest)

Aggregations

FileItem (org.apache.commons.fileupload.FileItem)3 IMultipartWebRequest (org.apache.wicket.protocol.http.IMultipartWebRequest)3 Request (org.apache.wicket.request.Request)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1