Upload taglib ver. 2.1


    Custom JSP tags. Lets you upload files onto your web server. Upload taglib provides more options in comparison with the similar stuff in the .NET framework. Multiple files can be uploaded in a single request. Taglib parses incoming request and extracts not only uploaded files but all request's parameters. For example:

1. Simple form: in HTML file you have some form with file HTML tag and multipart/form-data encoding:
 


<form method="post" enctype="multipart/form-data" action="upload.jsp">
  <input type=file size=20 name="fname">
  <input type=Submit value=Upload>
</form>

in file upload.jsp you will use this taglib:
 


<%@ taglib uri="taglib.tld" prefix="up" %>

<up:parse>
  File is: <up:getName/> Type is: <up:getType/>
  <up:saveFile path="c:/tmp" />
</up:parse>

You can save name in some page scope variable:
 


<up:parse>
  <up:getName id="A"/>
  Name is: <%=A%>
</up:parse>

Tag parse executes own body for the each uploaded file. So you may have more than one uploaded file:
 


<form method="post" enctype="multipart/form-data" action="upload.jsp">
<input type=file size=20 name="fname">
<input type=file size=20 name="fname1">
<input type=Submit value=Upload>
</form>

and of course tag's body may include any HTML (JSP) code. For example, you may decide protect overwriting etc. See File taglib also.

You may include other HTML tags in your form. Tag parse extracts them and they will be saved as attributes for your request. For example:
 


<form method="post" enctype="multipart/form-data" action="upload.jsp">
<input type=text size=20 name="description">
<input type=file size=20 name="fname">
<input type=Submit value=Upload>
</form>

Here is one additional text field description. In upload.jsp file you may use some like this:
 

<up:parse>
</up:parse>

Description is <%=request.getAttribute("description")%>

If some field may have multiple values than this call will return an array:
 


<%
  Object o=request.getAttribute("selection");
  if (o instanceof String[])
   {
    String selectedItems[]=(String[])o;
    ...
   }
  else
   {
    String selectedItem=(String)o;
    ...
   }
%>

How to proceed errors? What if you submit the above mentioned form without any file? In this case tag parse still extracts values for all other HTML fields but body will be skipped. You may use an additional parameter for this tag. This parameter describes a page scope variable (type is java.lang.Integer) and this variable will count uploaded files. So zero value signals about missed data. For example:
 


<up:parse id="A">
   ...
</up:parse>
<%=A%> files have been uploaded

Tags are:

isMultiPart

Body tag. Executes own body in case of multipart request. Parameters are: none

parse

Body tag. Parses POST request, adds extracted parameters to the request and executes own body for the each uploaded file. Parameters are:

1) id Optional parameter. Describes a page scope variable for counter (type is java.lang.Integer).

any tag described below is used in the context of parse tag:

getName

Reads and prints (saves in the page scope variable) file name for the current file. Parameters are:

1) id Optional parameter. Describes a page scope variable for data (type is java.lang.String). Without this parameter tag just prints data.

getType

Reads and prints (saves in the page scope variable) content type for the current file. Parameters are:

1) id Optional parameter. Describes a page scope variable for data (type is java.lang.String). Without this parameter tag just prints data.

saveFile

Saves current file. Parameters are:

1) path Optional parameter. Describes some file path or directory. In case of directory file will be saved under original name. By default (without this parameter) file will be saved in the current directory.
2) maxSize Optional parameter. Describes a maximal size (in bytes) for saved file. By default size is unlimited.
3) idSize Optional parameter. Describes a name for page scope variable (type is java.lang.Long). This variable will keep a size of saved file.
4) idName Optional parameter. Describes a name for page scope variable (type is java.lang.String). This variable will keep a full name of saved file.

Note: evaluation version restricts the size for uploaded file

for downloading:

Library: uptag.jar    Description: taglib.tld

Unrestricted version: Buy this taglib

© Coldbeans      Comments?

See also Coldtags suite - the largest collection of custom JSP tags.

Also in Coldtags: