[英]File upload to server upload directory by using ajax. The image is not getting uploading to upload directory; getting the error of connection reset
[英]Connection keeps getting reset when trying to upload file to tomcat server
我正在嘗試創建一個人們可以在其中發布內容的網站。 目前,我正在嘗試使用戶可以依次添加文件並將其作為多部分請求發送到ajax中。 我必須重新登錄服務器才能查看新文件。
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:
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) ;
});
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>
很有可能沒有任何代碼錯誤,但這是服務器配置問題。
默認情況下,大多數服務器都設置為較小的超時和較小的上傳大小限制。
這取決於您使用的服務器類型(nginx,Apache,Node / Express等)的默認設置以及如何配置它們,但這是我首先要研究的問題,如果您的連接正在重置或中斷中載。
我終於發現了問題。 您可能不會相信這一點,但是在一天結束時,表單中的按鈕仍然導致它被提交,從而導致整個過程混亂。 我要做的就是將type =“ button”添加到表單中的button元素。 現在,這阻止了表單的提交,僅使Ajax負責提交表單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.