<%& '/site/header/' %>

Forms with File Uploads

There are a few ways to deal with HTML forms in Prudence. In this example, we'll deal with it straightforwardly, using scriptlets.

We'll also handle file uploads, using the effective, and very simple API.


<% if (conversation.request.method == 'POST') { importClass(java.io.File) var name = conversation.form.get('name') var tmpAvatar = conversation.form.get('avatar').file var mediaType = conversation.form.get('avatar').mediaType // The metadata service can provide us with a default extension for the media type var extension = application.application.metadataService.getExtension(mediaType) // We will put all avatars under the /mapped/avatars/ directory, so that they // can be visible to the world var avatars = new File(document.source.basePath, 'avatars') avatars.mkdirs() var avatar = new File(avatars, name + '.' + extension) // Move the file to the new location tmpAvatar.renameTo(avatar) %>

Thank you for uploading your avatar, <%= name %>! Here's the image:

It was temporarily stored at:
<%= tmpAvatar %>

<% } else { %>

Your name:

Upload your avatar (an image file):

<% } %> <%& '/site/footer/' %>