开发者

JSP/Servlet: Create a form for uploading an unknown number of files

开发者 https://www.devze.com 2023-01-03 12:07 出处:网络
I need to create a form that will allow, among other things, a user to upload an unknown number of files to the server running Tomcat. It would be nice if the form could add upload slots to the form a

I need to create a form that will allow, among other things, a user to upload an unknown number of files to the server running Tomcat. It would be nice if the form could add upload slots to the form as ne开发者_Go百科eded. I haven't found much useful on the subject, so I thought I would poll the community.


You need an ajax enabled form that can add/remove input file elements:

<table border="1px">
    <tbody class="files">
    <tr><td><a class="delete" href="#">Delete</a></td><td><input class="fileSelect"  type="file" name="file" /></td></tr>
    <tr><td><a class="delete" href="#">Delete</a></td><td><input class="fileSelect"  type="file" name="file" /></td></tr>
    <tr><td><a class="delete" href="#">Delete</a></td><td><input class="fileSelect"  type="file" name="file" /></td></tr>
    <tr><td><a class="delete" href="#">Delete</a></td><td><input class="fileSelect"  type="file" name="file" /></td></tr>
    </tbody>
    <tbody>
    <tr><td><a class="add" href="#">Add</a></td><td><input id="submitButton" type="submit" value="Upload"/></td></tr>
    </tbody>
</table>

Using jQuery:

$(function() {
    $(".delete").live("click", function() {
      $(this).parent().parent().remove();
    });

    $(".add").click(function() {
      $("tbody.files").append("<tr><td><a class='delete' href='#'>Delete</a></td><td><input class='fileSelect' type='file' name='file' /></td></tr>");
    });
});

If you use Apache Commons FileUpload, then the server code is the same, no matter how many files are sent. FileUpload allows you to iterate over all uploaded files.


You could go with something like the Commons FileUpload Project

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号