Ber*_*ice 5 java jquery html5 servlets
我正在尝试jquery ajax使用输入类型文件和带有文本的普通输入类型发布帖子并从我的servlet中检索它们request.getParameter("element_name")但是当使用Chrome检查器时我发现我发送的FormData对象包含我的文件和我的文本值,servlet由于null某种原因正在读取参数.
这是我的形式:(ticket_id从另一个jsp成功返回)
<form id="upload-form" action="upload" enctype="multipart/form-data" method="post">
<input id="attach-btn" type="file" name="uploadedFile" style="display:none"/>
<input id="tick-id-upload" type="hidden" name="ID" value="<%=ticket_id%>" />
<input id="submit-form" type="button" style="display:none"/>
</form>
Run Code Online (Sandbox Code Playgroud)
这是jquery ajax的帖子:
// use to refresh section on submit of a form
$(document).on('click', '#submit-form', function()
{
var form_data = null;
if (drop === false)
{
// form data object with ticket id and file
form_data = new FormData($('#upload-form')[0]);
}
else
{
// append dropped file and value of id seperately
form_data = new FormData();
form_data.append('ID', $('#tick-id-upload').val());
form_data.append('uploadedFile', dropped_files);
}
$.ajax(
{
url: $('#upload-form').attr('action'),
type: 'POST',
async: true,
xhr: function() // custom XMLHttpRequest
{
myXhr = $.ajaxSettings.xhr();
if (myXhr.upload)
{ // check if upload property exists
myXhr.upload.addEventListener('progress', show_progress, false); // for handling the progress of the upload
}
return myXhr;
},
dataType: 'html', // the data type to be returned
success: function(response, status, xhr)
{
$('#progressbar').hide();
if (xhr.getResponseHeader('duplicate') === 'true')
{
// file is duplicate.. display dialog box
setTimeout(function()
{
$('#trigger-dialog').trigger('click');
}, 10);
}
else
{
// replace attachments section by section in response
$('#attachments').html($(response).find('#attachments').html());
execute_attach_datatable();
switch_to_view();
init_progress_bar();
override_section_height();
}
},
error: function(xhr, status, error)
{
alert(xhr.responseText);
},
data: form_data , // what data to pass
cache: false,
contentType: false, // type of data to be sent
processData: false
});
});
Run Code Online (Sandbox Code Playgroud)
这就是我在servlet中的doPost方法中所做的:
int ticket_id = Integer.parseInt(request.getParameter("ID"));
Run Code Online (Sandbox Code Playgroud)
NullPointerException虽然从Chrome中的"网络"部分看到的数据正在发送,但此行仍会返回.
请注意,在没有发送输入类型文本的情况下上传文件没有问题.即当我有没有tick-id-upload元素的相同表单,并使用相同的jquery ajax调用,文件上传成功.
对于发生了什么的任何想法?非常感谢!