blob: 2272950fd6d260a28b4e165aabaa496bfb4a436a [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2004, 2005 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<document>
<properties>
<title>Upload</title>
</properties>
<body>
<section name="Upload">
<p>
A form element used to handle file uploads. The uploaded file is represented by an
instance of
<a href="../../apidocs/org/apache/tapestry/request/IUploadFile.html">
IUploadFile
</a>
.
</p>
<p>
The maximum upload size of a file can be set by configuring the
<a
href="../../apidocs/org/apache/tapestry/multipart/MultipartDecoder.html">
MultipartDecoder
</a>
service in hivemind. The default is 10000000(10mb). The <code>maxSize</code> parameter is
specified in bytes, so you will have to give it the long form.
</p>
<section name="Example override of default maximum file upload size.">
<source xml:space="preserve">
&lt;implementation service-id="tapestry.multipart.ServletMultipartDecoder"&gt;
&lt;create-instance class="org.apache.tapestry.multipart.MultipartDecoderImpl,maxSize=-1" model="threaded" /&gt;
&lt;/implementation&gt;
</source>
</section>
<p>
<strong>
See also:
<a href="../../apidocs/org/apache/tapestry/form/Upload.html">
org.apache.tapestry.form.Upload
</a>
,
<a href="form.html">Form</a>
</strong>
,
<a
href="../../apidocs/org/apache/tapestry/multipart/ServletMultipartDecoder.html">
ServletMultipartDecoder
</a>
</p>
<section name="Parameters">
<table>
<tr>
<th>Name</th>
<th>Type</th>
<th>Required</th>
<th>Default</th>
<th>Description</th>
</tr>
<tr>
<td>file</td>
<td>
<a
href="../../apidocs/org/apache/tapestry/request/IUploadFile.html">
IUploadFile
</a>
</td>
<td>yes</td>
<td></td>
<td>
Updated, when the form is submitted, with the name and content uploaded.
</td>
</tr>
<tr>
<td>disabled</td>
<td>boolean</td>
<td>no</td>
<td>false</td>
<td>
If true, then (on render) the "disabled" attribute is written into the
tag and on submit, the upload will not update its file parameter.
</td>
</tr>
<tr>
<td>displayName</td>
<td>string</td>
<td>no</td>
<td></td>
<td>
The user-presentable name for the component, which will be used by a
<a href="fieldlabel.html">FieldLabel</a>
connected to the component.
</td>
</tr>
<tr>
<td>validators</td>
<td>
Array or collection of
<a
href="../../apidocs/org/apache/tapestry/form/validator/Validator.html">
Validator
</a>
</td>
<td>no</td>
<td></td>
<td>
The validators to apply to the component. Something along the lines of:
<code>validators:required</code> .<br/><br/>
<strong>See also:</strong> <a href="../../usersguide/validation.html">Validation</a>
</td>
</tr>
<tr>
<td>id</td>
<td>String</td>
<td>no</td>
<td></td>
<td>Sets the id attribute for the rendered <code>&lt;input&gt;</code> element.</td>
</tr>
</table>
<p>
Body:
<strong>removed</strong>
</p>
<p>
Informal parameters:
<strong>allowed</strong>
</p>
<p>Reserved parameters: name, type</p>
</section>
<section name="Examples">
<p>UploadPage.html</p>
<source xml:space="preserve"><![CDATA[
<form jwcid="@Form" listener="listener:formSubmit">
<label jwcid="@FieldLabel" field="component:upload">File</label>
<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 &amp;&amp; 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>
]]></source>
<p>UploadPage.java</p>
<source xml:space="preserve">
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 &lt; 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) {}
}
}
}
}
</source>
</section>
</section>
</body>
</document>