简体   繁体   English

多部分/表单数据表单提交

[英]Multipart/form-data form submit

In IE9, when I submit a form (enc-type: multipart/form-data) with a file to a java servlet something goes wrong. 在IE9中,当我向Java Servlet提交带有文件的表单(enc类型:multipart / form-data)时,出现了问题。 I have trouble locating the error and finding a solution. 我很难找到错误并找到解决方案。 The Content-Length is 0 when it's not working. 不工作时,Content-Length为0。 That's the only difference in the request header. 这是请求标头中的唯一区别。 And there is no difference in the request message. 并且请求消息中没有任何区别。

How I try to retrieve the form data in java serlvet (using org.apache.commons): 我如何尝试在java serlvet中检索表单数据(使用org.apache.commons):

ServletFileUpload upload = new ServletFileUpload();
if (!ServletFileUpload.isMultipartContent(request)
    throw new Exception("Invalid parameters");
FileItemIterator itr = upload.getItemIterator(request);
while(itr.hasNext()
{
    // This never gets run when the error occurs (Content-Length: 0).. 
}

I assume there is something wrong with the form submit? 我认为表单提交有问题吗? Does anyone have any idea what could be wrong? 有人知道什么地方可能出问题吗? In chrome it allways works. 在Chrome中,它始终有效。

UPDATE: essential parts of html form: 更新:html表单的基本部分:

<form name='uploadparticipants' action='ParticipantUploader' method='post' encoding='multipart/form-data' enctype='multipart/form-data' target='upload_target' onsubmit='admin.uploadCourseParticipants()'>
// Some input fields inside a table, among them a file input //
<input type='submit' value='somevalue'>
</form>
  1. Please make sure you have explicitly added method="post" during form creation. 请确保您在表单创建过程中已明确添加method="post"
  2. Also, when creating forms dynamically in Internet Explorer (both 6 and 7) using Javascript, you need to also set the encoding attribute, for some reason. 另外,当使用Javascript在Internet Explorer(6和7)中动态创建表单时,由于某种原因,还需要设置编码属性。 So although the attribute is called enctype, setting the enctype does nothing. 因此,尽管该属性称为enctype,但是设置enctype不会执行任何操作。 Please try adding the following code: 请尝试添加以下代码:

    form.setAttribute('encoding', 'multipart/form-data'); form.setAttribute('encoding','multipart / form-data');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM