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 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
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?
解决方案
Maybe it's a typo but you are missing a "name" attribute, wich is mandatory.
After some testing, without a name in the field, the file input is not included in the List. 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.