| <!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"> </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> |
| <extension name="org.apache.tapestry.multipart-decoder" |
| class="org.apache.tapestry.multipart.DefaultMultipartDecoder"> |
| <configure property-name="maxSize" type="double" value="134217728"/> |
| <!-- where the value is the maximum size you want to allow --> |
| </extension> |
| </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> </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="examples" cellpadding="4"> |
| <tr> |
| <td colspan="2">File: <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 bytes</td> |
| </tr> |
| </table> |
| </form> |
| |
| <pre> |
| <form jwcid="@<a href="Form.html">Form</a>" listener="ognl:listener.formSubmit"> |
| <table bgcolor="#c0c0c0" cellpadding="4"> |
| <tr> |
| <td colspan="2">File:&nbsp;<input jwcid="<span class="jwcid">@<a href="Upload.html">Upload</a></span>" file="ognl:file" type= "file"></input></td> |
| </tr> |
| <tr> |
| <td colspan="2"><input type= "submit"value="Upload"></input></td> |
| </tr> |
| <tr> |
| <td colspan="2"><hr></td> |
| </tr> |
| <tr> |
| <td>Filename:</td><td><span jwcid="@<a href="Insert.html">Insert</a>" value="ognl:filename"/>/td> |
| </tr> |
| <tr> |
| <td>Client path:</td><td><span jwcid="@<a href="Insert.html">Insert</a>" value="ognl:clientPath"/></td> |
| </tr> |
| <tr> |
| <td>Server Path:</td><td><span jwcid="@<a href="Insert.html">Insert</a>" value="ognl:serverPath"/></td> |
| </tr> |
| <tr> |
| <td>File Truncated:</td><td><span jwcid="@<a href="Insert.html">Insert</a>" value="ognl:fileTruncated"/></td> |
| </tr> |
| <tr> |
| <td>File Size:</td><td><span jwcid="@<a href="Insert.html">Insert</a>" value="ognl:fileSize" format="ognl:@UploadPage@SIZE_FORMAT"/>&nbsp;bytes</td> |
| </tr> |
| </table> |
| </form> |
| |
| |
| |
| 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 < 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> |