FileUpload isFormField()在提交文件时返回true [英] FileUpload isFormField() returning true when submitting file
问题描述
我使用 Apache-Commons FileUpload库
将文件上传到服务器。它工作正常,但突然当我提交文件时,由于某种原因, FileItem.isFormField()
返回true。这是我的代码
FileUpload.java servlet
if(ServletFileUpload.isMultipartContent(request))
{
List< FileItem> items = new ServletFileUpload(
new DiskFileItemFactory())。parseRequest(request);
for(FileItem item:items)
{
// if item是文件类型而不是表单域
if(!item.isFormField())
{
//上传文件
}
}
}
ticketform.jsp
< form action =uploadenctype =multipart / form-datamethod = post>
< button type =buttonid =clip-btnclass =attach-tool-tip>
< img src =images / attachment.pngid =attach-imgwidth =25px/>
< / button>
< input id =attach-btntype =filestyle =display:none/>
< input id =submit-formname =uploadtype =submitstyle =display:none/>
< / form>
ticketform.js
//触发文件选择器点击回形针图标时单击
$('#clip-btn')。click(function()
{
$('#attach-btn')。trigger('click');
});
//触发文件提交文件名在输入类型变更='文件'
$('#attach-btn')。change(function()
{
$('#submit-form')。trigger('click');
});
当我看到' attach-btn
'即输入文件类型,该文件在那里是正确的名称,最后修改,大小等等。所以它提交了良好的文件。有没有什么外部原因,为什么当请求被解析时,它是作为一个表单字段计数的?解析方案
可能这是一个错字,但是
< input id =attach-btntype = filename =someFilestyle =display:none/>
经过一些测试,在字段中没有名称的情况下,文件输入
不包含在列表< FileItem>
中。你只要收到提交输入
的默认值(在我的情况下就像是发送请求)。
试试吧,告诉我们它是否有效。
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?
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.
这篇关于FileUpload isFormField()在提交文件时返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!