FileUpload isFormField() returning true when submi

2019-07-02 01:38发布

I am using Apache-Commons FileUpload library to upload files to a server. It was working fine, but suddenly when I am submitting the file, FileItem.isFormField() is returning true for some reason or another. This is the code I have

FileUpload.java servlet

if (ServletFileUpload.isMultipartContent(request)) 
{
    List<FileItem> items = new ServletFileUpload(
                   new DiskFileItemFactory()).parseRequest(request);

    for (FileItem item : items)
    {
        // if item is a file type and not a form field 
        if (!item.isFormField())
        {
                // UPLOAD FILE
        }
    }
}

ticketform.jsp

<form action="upload" enctype="multipart/form-data" method=post>
    <button type="button" id="clip-btn" class="attach-tool-tip" >
        <img src="images/attachment.png" id="attach-img" width="25px"/>
    </button>

    <input id="attach-btn" type="file" style="display:none"/>
    <input id="submit-form" name="upload" type="submit" style="display:none"/>
</form>

ticketform.js

// trigger file chooser click when clicking paper clip icon
$('#clip-btn').click(function()
{
    $('#attach-btn').trigger('click');
});

// trigger file submit on filename change in input type='file'
$('#attach-btn').change(function()
{
    $('#submit-form').trigger('click');
});

When I am seeing the contents of 'attach-btn' i.e. the input file type, the file is there with it's correct name, last modified, size etc.. so it is submitting with the good file. Can there be any external reason why when the request is parsed, it's counting as a form field?

1条回答
放我归山
2楼-- · 2019-07-02 02:07

Maybe it's a typo but you are missing a "name" attribute, wich is mandatory.

<input id="attach-btn" type="file" name="someFile" style="display:none"/>

After some testing, without a name in the field, the file input is not included in the List<FileItem>. You recieve just the submit input with a default value (in my case something like "send request").

Try it and tell us if it worked.

查看更多
登录 后发表回答