[英]Connection keeps getting reset when trying to upload file to tomcat server
I am trying to create a website where people can post stuff. 我正在尝试创建一个人们可以在其中发布内容的网站。 Currently, I am trying to make it such that users can add files sequentially and send it as a multi-part request in ajax.However, I keep getting connection reset error in chrome when I am trying to upload files.
目前,我正在尝试使用户可以依次添加文件并将其作为多部分请求发送到ajax中。 I have to log back into the server in order to see the new files.
我必须重新登录服务器才能查看新文件。
HTML code: HTML代码:
<form id="postForm" method="post" enctype="multipart/form-data">
<textarea name="text" rows="4" cols="" class="form-control" placeholder="What's Up" id="postTextarea"></textarea>
<div class="radio">
<label><input type="radio" name="privacy" value="restricted" checked>Restricted</label>
<label class="checkbox-inline"><input type="checkbox" name="group" value="family" class="group">Family</label>
<label class="checkbox-inline"><input type="checkbox" name="group" value="bestie" class="group">Besties</label>
<label class="checkbox-inline"><input type="checkbox" name="group" value="friend" class="group">Friends</label>
</div>
<div class="radio">
<label><input type="radio" name="privacy" value="public">Public</label>
</div>
<br>
<div id="fileContainer">
</div>
<div id="buttonContainer">
<label class="btn btn-default" id="fileUploadButton">
Add Files<input type="file" name="file" class="fileInput" hidden multiple>
</label>
<button class="btn btn-primary" id="postButton">Post</button>
</div>
</form>
JavaScript: JavaScript:
var submittedFileData = [] ;
$('.fileInput').on('change', function () {
var files = $(this).prop('files') ;
for (var i = 0; i < files.length; i++) {
var p = document.createElement("p") ;
$(p).text(files[i].name + " (Click here to remove file)") ;
$(p).addClass("filename") ;
$("#fileContainer").append(p) ;
submittedFileData.push(files[i]);
}
});
$('#postButton').click(function () {
var privacy = $( "input[type=radio][name=privacy]:checked" ).val();
var formData = new FormData();
//append privacy
formData.append("privacy" , privacy) ;
//append groups
$( "input[type=checkbox][name=group]:checked" ).each(function(i) {
formData.append("group" , $(this).val()) ;
}) ;
//append text
formData.append("text" , $("#postTextarea").val()) ;
//append files
for (var i = 0; i < submittedFileData.length; i++) {
formData.append("file" , submittedFileData[i]) ;
}
$.ajax({
type: "POST",
url: "post-upload",
data: formData,
processData: false,
contentType: false,
cache: false,
async : false,
success: function (data) {
},
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown) ;
}
});
$.post("feed-view","relation="+activeTab ,getXMLResponse) ;
});
server.xml for tomcat 9 Tomcat 9的server.xml
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
sslImplementationName="org.apache.tomcat.util.net.jsse.JSSEImplementation"
maxThreads="150" SSLEnabled="true"
scheme="https"
secure="true" maxPostSize="-1" disableUploadTimeout="false" connectionUploadTimeout="600000">
<SSLHostConfig>
<Certificate certificateKeystoreFile="C:/Users/Kathavarayan/.keystore"
certificateKeystorePassword=""
type="RSA" />
</SSLHostConfig>
</Connector>
It's very likely that there isn't anything code-wise that is wrong, but instead it is a server configuration issue. 很有可能没有任何代码错误,但这是服务器配置问题。
By default, most servers are set to both rather small timeouts and rather small upload size limits. 默认情况下,大多数服务器都设置为较小的超时和较小的上传大小限制。
It depends on what type of server you are using (nginx, Apache, Node/Express, etc) for what the defaults are and how to configure them, but that would be the first thing I look into if your connection is resetting or cutting off mid-upload. 这取决于您使用的服务器类型(nginx,Apache,Node / Express等)的默认设置以及如何配置它们,但这是我首先要研究的问题,如果您的连接正在重置或中断中载。
I finally discovered the problem. 我终于发现了问题。 You are probably not going to believe this but at the end of the day, the button in the form was still causing it to be submitted causing the entire process to mess up.
您可能不会相信这一点,但是在一天结束时,表单中的按钮仍然导致它被提交,从而导致整个过程混乱。 All I had to do was add type="button" to the button element in the form.
我要做的就是将type =“ button”添加到表单中的button元素。 Now, this prevents the form from being submitted and only causes Ajax to take care of submitting the form.
现在,这阻止了表单的提交,仅使Ajax负责提交表单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.