TextField Component Index ValidField

Upload
net.sf.tapestry.form.Upload
 
Description
A form element used to handle file uploads. The uploaded file is represented by an instance of IUploadFile.

Note the form's enctype must be set to multipart/form-data, i.e. <form enctype="multipart/form-data">.

See Also
Form
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. Note Netscape Navigator 4 will ignore this attribute.

Body: removed
Informal parameters: allowed
Reserved parameters: "name", "type"
Examples

Provides a file upload form and displays the results of the upload.

To save the uploaded file to the WEB context path, rather than the default java path, use the ServletContext.getRealPath() method to obtain the web application's directory path.

File: 

File Name:mybook.txt
Client Path:C:\mybook.txt
Server Path:C:\Program Files\Tomcat 4.0.4\bin\mybook.txt
File Truncated:false
File Size:2,399 bytes
<form jwcid="form" enctype="multipart/form-data">
 <table bgcolor="#c0c0c0" cellpadding="4">
  <tr>
   <td colspan="2">File:&nbsp;<input jwcid="fileUpload" 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="insertFileName"/>/td>
  </tr>
  <tr>
   <td>Client path:</td><td><span jwcid="insertClientPath"/></td>
  </tr>
  <tr>
   <td>Server Path:</td><td><span jwcid="insertServerPath"/></td>
  </tr>
  <tr>
   <td>File Truncated:</td><td><span jwcid="insertFileTruncated"/></td>
  </tr>
  <tr>
   <td>File Size:</td><td><span jwcid="insertFileSize"/>&nbsp;bytes</td>
  </tr>
 </table>
</form>


<component id="form" type="Form">
   <binding name="listener" expression="listener.formSubmit"/>
</component>

<component id="fileUpload" type="Upload">
   <binding name="file" expression="file"/>
</component>

<component id="insertFileName" type="Insert">
    <binding name="value" expression="filename"/>
</component>

<component id="insertClientPath" type="Insert">
    <binding name="value" expression="clientPath"/>
</component>

<component id="insertServerPath" type="Insert">
    <binding name="value" expression="serverPath"/>
</component>

<component id="insertFileTruncated" type="Insert">
    <binding name="value" expression="fileTruncated"/>
</component>

<component id="insertFileSize" type="Insert">
    <binding name="value" expression="fileSize"/>
    <binding name="format" expression="@UploadPage@SIZE_FORMAT"/>
</component>


public class UploadPage extends BasePage {
    public static final Format SIZE_FORMAT = NumberFormat.getNumberInstance();    
    private IUploadFile 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) {}
            }
        }
    }

    public void detach() {
        file = null;
        serverFile = null;
        super.detach();
    }
}

TextField Component Index ValidField