Upload
A form element used to handle file uploads. The uploaded file is represented by an instance of IUploadFile.
See also: org.apache.tapestry.form.Upload, Form
Warning
This component may only be used in Tapestry Servlet Applications; Upload currently
does not work with Portlet Applications. See
TAPESTRY-635 for more details.
Parameters
Name | Type | Direction | Required | Default | Description |
---|---|---|---|---|---|
file | IUploadFile | out | yes | Updated, when the form is submitted, with the name and content uploaded. | |
disabled | boolean | in | no | false | If true, then (on render) the "disabled" attribute is written into the tag and on submit, the upload will not update its file parameter. |
displayName | string | in | no | The user-presentable name for the component, which will be used by a FieldLabel connected to the component. | |
validators | Array or collection of Validator, or Validator | in | no | The validators to apply to the component. | |
id | String | in | no | Sets the id attribute for the rendered <input> element. |
Body: removed
Informal parameters: allowed
Reserved parameters: name, type
Examples
UploadPage.html
<form jwcid="@Form" listener="listener:formSubmit"> <span jwcid="@FieldLabel" field="component:upload"/> <input jwcid="upload@Upload" file="ognl:uploadFile" type="file" displayName="File" validators="validators:required"/> <input type= "submit" value="Upload"/> <span jwcid="@If" condition="ognl: uploadFile && serverFile"> <ul> <li>Filename: <span jwcid="@Insert" value="ognl:uploadFile.fileName"/></li> <li>Client path: <span jwcid="@Insert" value="ognl:uploadFile.filePath"/></li> <li>Server Path: <span jwcid="@Insert" value="ognl:serverFile.absolutePath"/></li> <li>File Size: <span jwcid="@Insert" value="ognl:serverFile.length()" format="ognl:numberFormat"/> bytes</li> </ul> </span> </form>
UploadPage.java
public abstract class UploadPage extends BasePage { public abstract IUploadFile getUploadFile(); public abstract File getServerFile(); public abstract void setServerFile(File file); public Format getNumberFormat() { return NumberFormat.getNumberInstance(); } public void formSubmit(IRequestCycle cycle) { if (getUploadFile() == null) { return; } InputStream fis = getUploadFile().getStream(); FileOutputStream fos = null; try { fos = new FileOutputStream(new File(getUploadFile().getFileName())); byte[] buffer = new byte[1024]; while (true) { int length = fis.read(buffer); if (length < 0) { break; } fos.write(buffer, 0, length); } fis.close(); fos.close(); setServerFile(new File(getUploadFile().getFileName())); } catch (IOException ioe) { ioe.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException ioe) {} } if (fos != null) { try { fos.close(); } catch (IOException ioe) {} } } } }