


Wicket supports file uploading with the FileUploadField component which must be used with the <input> tag whose type attribute must be set to "file" In order to send a file on form submission we must enable multipart mode calling setMultiPart(true) on our form.

In the next example (project UploadSingleFile) we will see a form which allows users to upload a file into the temporary directory of the server (path /tmp on Unix/Linux systems):

*HTML:*

[source,html]
----
<html>
	<head>
	</head>
	<body>
		<h1>Upload your file here!</h1>
		<form wicket:id="form">
			<input type="file" wicket:id="fileUploadField"/> 
			<input type="submit" value="Upload"/>
		</form>
		<div wicket:id="feedbackPanel">
		</div>
	</body>
</html>
----

*Java code:*

[source,java]
----
public class HomePage extends BootstrapBasePage {
    private FileUploadField fileUploadField;

    public HomePage(final PageParameters parameters) {
        
        fileUploadField = new FileUploadField("fileUploadField");
        
        Form<Void> form = new Form<Void>("form"){
            @Override
            protected void onSubmit() {
              super.onSubmit();
                 
              FileUpload fileUpload = fileUploadField.getFileUpload();
                
                try {
                    File file = new File(System.getProperty("java.io.tmpdir") + "/" +
                            fileUpload.getClientFileName());
                    
                    fileUpload.writeTo(file);
                    info("Upload completed!");
                } catch (Exception e) {
                    e.printStackTrace();
                    error("Upload failed!");
             }
            }
        };  
    
        form.setMultiPart(true);
        //set a limit for uploaded file's size
        form.setMaxSize(Bytes.kilobytes(100));
        form.add(fileUploadField);
        add(new FeedbackPanel("feedbackPanel"));
        add(form);
    }
}
----

The code that copies the uploaded file to the temporary directory is inside the onSubmit method of the Form class. The uploaded file is handled with an instance of class FileUpload returned by the  getFileUpload() method of the FileUploadField class. This class provides a set of methods to perform some common tasks like getting the name of the uploaded file (getClientFileName()), coping the file into a directory (writeTo(destinationFile)), calculating file digest (getDigest (digestAlgorithm)) and so on.

Form component can limit the size for uploaded files using its setMaxSize(size) method. In the example we have set this limit to 100 kb to prevent users from uploading files bigger than this size.

NOTE: The maximum size for uploaded files can also be set at application's level using the setDefaultMaximumUploadSize(Bytes maxSize) method of class ApplicationSettings:

[source,java]
----
@Override
public void init()
{
	getApplicationSettings().setDefaultMaximumUploadSize(Bytes.kilobytes(100));  
}
----

=== Upload multiple files

If we need to upload multiple files at once and our clients support HTML5, we can still use FileUploadField adding attribute "multiple" to its tag. If we can not rely on HTML5, we can use the MultiFileUploadField component which allows the user to upload an arbitrary number of files using a JavaScript-based solution.
An example showing how to use this component can be found in Wicket module wicket-examples in file MultiUploadPage.java. The live example is hosted on the {wicket_examples_url}/upload/multi[examples site].

