FileUpload isFormField() returning true when submitting file

10,260

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.

Share:
10,260
Bernice
Author by

Bernice

Mostly interested in web development and Java. life motto: while(!successful) { tryAgain(); }

Updated on June 04, 2022

Comments

  • Bernice
    Bernice almost 2 years

    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?