2) Upload Multiple Files.

 

back to main index

 

To upload multiple files using Xload the procedure is simple. Just add the file parameters to the request and provide ‘targeting’ for these parameters on the server side and Xload will do the rest. For example, use the following code to upload three different files to three different locations.

 

 

XloadManager xman = new XloadManager(request);

xman.target("file1", "uploaded1");

xman.target("file2", "uploaded2");

xman.target("file3", "uploaded3");

xman.upload();

 

 

 

 

where:

request - HttpServletRequest object.

uploaded* - Directories to upload files to (relative to the web application directory).

file* - File parameter inside html (or other) form.

     

     

The html on the client side would look like:

 

 

<form action="/my_servlet" enctype="multipart/form-data" method="post">

          file1: <input type="file" name="file1">

          file2: <input type="file" name="file2">

          file3: <input type="file" name="file3">

</form>

 

 

 

 

The above code snippets would upload the three files to their individual directories using their original ('remote') names (unless a file name collision occurs in which case the default rename policy comes into effect in this case). This code does not specify any maximum sizes for the files or other constraining parameters such as allowable MIME types. This means that defaults are used, which is a maximum 1GB file size and any MIME type is allowed to be uploaded.

 

 

IMPORTANT:- You cannot have two file parameters inside the request form with the same name as this will cause Xload to throw an exception and is semantically incorrect.

 

IMPORTANT:-  You can set targets for files that do not exist in the request, they simply will not be used and become 'redundant'. Also, Xload does allow you to send files in the request without any specific targeting existing within Xload. Xload responds by just ignoring these files and moving onto the next part of the request. Although this may seem as though this situation should never arise it was allowed to give total flexibility.

 

 

The code below shows how to discover if any of your targets have not been used in the upload process. Notice that file2 request parameter has a specific target but does not exist in the request html form below.

 

 

XloadManager xman = new XloadManager(request);

xman.target("file1", "uploaded1");

xman.target("file2", "uploaded2");

 

xman.upload();

Iterator it = xman.getRedundantTargetUploads();

while(it.hasNext()){

XloadFileUpload redundant = (XloadFileUpload)it.next();

//deal with redundant which will be file2

}

 

 

 

 

where:

request - HttpServletRequest object.

uploaded* - Directories to upload files to (relative to the web application directory).

file* - File parameter inside html (or other) form.

     

     

The html on the client side would look like:

 

 

<form action="/my_servlet" enctype="multipart/form-data" method="post">

          file1: <input type="file" name="file1">        

</form>

 

 

 

 

The code below shows how to discover if any file parameters have more than one target. The hasMultipleTargets() method can be called at any time and takes into account any targeting that would be set by the use of the targetAll() and targetEverything() family of methods of the XloadManager class.

 

 

XloadManager xman = new XloadManager(request);

xman.target("file1", "uploaded1");

xman.hasMultipleTargets("file1"); //returns false

xman.target("file2", "uploaded2");

xman.hasMultipleTargets("file2"); //returns false

xman.target("file2", "uploaded3");

xman.upload();

xman.hasMultipleTargets("file2"); //returns true

 

 

 

 

where:

request - HttpServletRequest object.

uploaded* - Directories to upload files to (relative to the web application directory).

file* - File parameter inside html (or other) form.

     

 

back to main index

top of page

 

 

 

 

 

© Gubutech(Xload) 2006  (v1.2)