Upload

A form element used to handle file uploads. The uploaded file is represented by an instance of IUploadFile .

The maximum upload size of a file can be set by configuring the MultipartDecoder service in hivemind. The default is 10000000(10mb). The maxSize parameter is specified in bytes, so you will have to give it the long form.

Example override of default maximum file upload size.

<implementation service-id="tapestry.multipart.ServletMultipartDecoder">
<create-instance class="org.apache.tapestry.multipart.MultipartDecoderImpl,maxSize=-1" model="threaded" />
</implementation> 

See also: org.apache.tapestry.form.Upload , Form , ServletMultipartDecoder

Parameters

Name Type Required Default Description
file IUploadFile yes Updated, when the form is submitted, with the name and content uploaded.
disabled boolean 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 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 no The validators to apply to the component. Something along the lines of: validators:required .



See also: Validation
id String 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) {}
          }
      }
  }

}