doc/src/ComponentReference/Upload.html (271 lines of code) (raw):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- $Id$ --> <html> <head> <title>Upload</title> <link rel="stylesheet" type="text/css" href="Tapestry.css" title="style"> </head> <body> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td align="left"><A href="TextField.html"><IMG alt=TextField src="common-images/prev.png"></a></td> <td align="middle"><A href="index.html"><IMG alt="Component Index" src="common-images/home.png" ></a></td> <td align="right"><A href="ValidField.html"><IMG alt=ValidField src="common-images/next.png"></a></td> <tr> <tr> <td colspan="3"><hr></td> </tr> <tr> <td colspan="3"> <table border="0" cellpadding="4" cellspacing="4" width="100%"> <tr valign="top"> <td> <table> <tr> <td><font size="+2"><b>Upload</b></font></td> </tr> <tr> <td> <A href="../api/org/apache/tapestry/form/Upload.html"> org.apache.tapestry.form.Upload</a> </td> </tr> </table> </td> <td> <table align="right" valign="middle" bgcolor="#c0c0c0" cellpadding="8"> <tr> <td><input type="file"></td> </tr> </table> </td> </tr> <tr valign="center"> <td colspan="2">&nbsp;</td> </tr> <tr> <td colspan="2"> <b>Description</b> <br> A form element used to handle file uploads. The uploaded file is represented by an instance of <a href="../api/org/apache/tapestry/request/IUploadFile.html">IUploadFile</a>. By default the upload component limits the size of uploads to 10,000,000 bytes. To alter this limit you can add the following to your *.application specification: <pre> &lt;extension name="org.apache.tapestry.multipart-decoder" class="org.apache.tapestry.multipart.DefaultMultipartDecoder"&gt; &lt;configure property-name="maxSize" type="double" value="134217728"/&gt; &lt;!-- where the value is the maximum size you want to allow --&gt; &lt;/extension&gt; </pre> <br> Alternatively you can use: <pre> DefaultMultipartDecoder.getSharedInstance().setMaxSize(12345678); </pre> <br> Setting the size to -1 will remove any limit checking. </td> </tr> <tr> <td colspan="2"> <b>See Also</b> <br> <A href="Form.html">Form</a> </td> </tr> <tr> <td colspan="2"> <b>Parameters</b> <br> <table border="1" cellpadding="4" cellspacing="4" class="parameters"> <tr> <th>Name</th> <th>Type</th> <th>Direction</th> <th>Required</th> <th>Default</th> <th>Description</th> </tr> <tr> <td>file</td> <td><A href="../api/org/apache/tapestry/request/IUploadFile.html"><tt> IUploadFile</tt></a> </td> <td>out</td> <td>yes</td> <td>&nbsp;</td> <td>Updated, when the form is submitted, with the name and content uploaded. </td> </tr> <tr> <td>disabled</td> <td>boolean</td> <td>in</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. Note Netscape Navigator 4 will ignore this attribute. </td> </tr> </table><BR>Body: <STRONG>removed</STRONG><BR> Informal parameters: <STRONG>allowed</STRONG><br> Reserved parameters: "name", "type" </td> </tr> <tr> <td colspan="2"> <b>Examples</b> <p> Provides a file upload form and displays the results of the upload. <p> To save the uploaded file to the WEB context path, rather than the default <tt>java</tt> path, use the <tt>ServletContext.getRealPath()</tt> method to obtain the web application's directory path. <form> <table class="tapestry-examples" cellpadding="4"> <tr> <td colspan="2">File:&nbsp;<input type="file"></td> </tr> <tr> <td colspan="2"><input type="submit" value="Upload"></td> </tr> <tr> <td colspan="2"><hr></td> </tr> <tr> <td>File Name:</td><td>mybook.txt</td> </tr> <tr> <td>Client Path:</td><td>C:\mybook.txt</td> </tr> <tr> <td>Server Path:</td><td>C:\Program Files\Tomcat 4.0.4\bin\mybook.txt</td> </tr> <tr> <td>File Truncated:</td><td>false</td> </tr> <tr> <td>File Size:</td><td>2,399&nbsp;bytes</td> </tr> </table> </form> <pre> &lt;form jwcid="@<a href="Form.html">Form</a>" listener="ognl:listener.formSubmit"&gt; &lt;table bgcolor="#c0c0c0" cellpadding="4"&gt; &lt;tr&gt; &lt;td colspan="2"&gt;File:&amp;nbsp;&lt;input jwcid="<span class="jwcid">@<a href="Upload.html">Upload</a></span>" file="ognl:file" type= "file"&gt;&lt;/input&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&lt;input type= "submit"value="Upload"&gt;&lt;/input&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="2"&gt;&lt;hr&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Filename:&lt;/td&gt;&lt;td&gt;&lt;span jwcid="@<a href="Insert.html">Insert</a>" value="ognl:filename"/&gt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Client path:&lt;/td&gt;&lt;td&gt;&lt;span jwcid="@<a href="Insert.html">Insert</a>" value="ognl:clientPath"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Server Path:&lt;/td&gt;&lt;td&gt;&lt;span jwcid="@<a href="Insert.html">Insert</a>" value="ognl:serverPath"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;File Truncated:&lt;/td&gt;&lt;td&gt;&lt;span jwcid="@<a href="Insert.html">Insert</a>" value="ognl:fileTruncated"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;File Size:&lt;/td&gt;&lt;td&gt;&lt;span jwcid="@<a href="Insert.html">Insert</a>" value="ognl:fileSize" format="ognl:@UploadPage@SIZE_FORMAT"/&gt;&amp;nbsp;bytes&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; public class UploadPage extends BasePage { public static final Format SIZE_FORMAT = NumberFormat.getNumberInstance(); private <a href="../api/org/apache/tapestry/request/IUploadFile.html">IUploadFile</a> file; private File serverFile; public IUploadFile getFile() { return file; } public void setFile(IUploadFile value) { file = value; } public String getFilename() { if (file != null) { return file.getFileName(); } else { return ""; } } public String getClientPath() { if (file != null) { return file.getFilePath(); } else { return ""; } } public String getServerPath() { if (serverFile != null) { return serverFile.getAbsolutePath(); } else { return ""; } } public long getFileSize() { if (serverFile != null) { return serverFile.length(); } else { return 0; } } public boolean isFileTruncated() { if (file != null) { return file.isTruncated(); } else { return false; } } public void formSubmit(IRequestCycle cycle) { InputStream fis = file.getStream(); FileOutputStream fos = null; try { fos = new FileOutputStream(new File(file.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(); serverFile = new File(file.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) {} } } } protected void initialize() { file = null; serverFile = null; } } </pre> </td> </tr></table> </td></tr> <tr> <td colspan="3"><hr></td> </tr> <tr> <td align="left"><A href="TextField.html"><IMG alt=TextField src="common-images/prev.png"></a></td> <td align="middle"><A href="index.html"><IMG alt="Component Index" src="common-images/home.png" ></a></td> <td align="right"><A href="ValidField.html"><IMG alt=ValidField src="common-images/next.png"></a></td> </tr> </table> </body> </html>