XMLHttpRequest :: ERR_CONNECTION_RESET when uploading large files (2 Mo & More)
Problem
I'm trying to upload a file using XMLHttpRequest and it only seems to work with a small file (up to 2MO for example). I've tried a lot of things so far and ended up with the code shown at the end of the post. But there is nothing to do; I keep getting the error ::ERR_CONNECTION_RESET
. This is not a problem with the code as when uploading files 2MO loads correctly ... What am I forgetting? I know this is probably an IIS or web.config issue, but I can get my finger on it going through this issue.
Error provided by Chrome
POST WEBSITEANDSERVICEURL / Service / MyService.asmx / UploadFilesWithAJAX net :: ERR_CONNECTION_RESET
- handleFileSelect
- x.event.dispatch
- v.handle
Javascript
<script type="text/javascript">
$(function() {
$('#<%= files.ClientID %>').change(handleFileSelect);
});
function handleFileSelect(e) {
if (!e.target.files || !window.FileReader) return;
var fd = new FormData();
var file = document.getElementById("<%= files.ClientID %>");
for (var i = 0; i < file.files.length; i++) {
fd.append('_file', file.files[i]);
}
var xhr = new XMLHttpRequest();
xhr.open('POST', '<%= ResolveURL("~/Services/MyService.asmx/UploadFilesWithAJAX") %>', true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
alert(xhr.responseText);
}
};
xhr.upload.addEventListener("progress", updateProgress, false);
xhr.send(fd);
}
function updateProgress(oEvent) {
if (oEvent.lengthComputable) {
var percentComplete = oEvent.loaded / oEvent.total;
$("#progress").text(oEvent.loaded + " ON " + oEvent.total);
}
}
</script>
HTML markup
<asp:FileUpload ID="files" runat="server" multiple="true" />
<br />
<table id="selectedFiles">
</table>
<span id="progress"></span>
MyService.asmx
<ScriptService()> _
<ToolboxItem(False)> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
Public Class AJAXServices : Inherits WebService
<WebMethod(EnableSession:=True)> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Xml)> _
Public Function UploadFilesWithAJAX()
' Some code which work fine, I'm sure of it.
End Function
End Class
Web.config
<!-- [...] -->
<system.web>
<httpRuntime maxRequestLength="2097151" executionTimeout="180" /><!-- MAXIMUM DURING TESTING -->
<!-- [...] -->
</system.web>
<!-- [...] -->
source to share
Ok I solved it ...
Decision
If this happens to someone else, be sure to get at least one access Context.Request.Files
in your WebService.
Because during my tests, I simply:
Public Function UploadFilesWithAJAX()
Return "RETURN SOMETHING"
End Function
But that wasn't enough ... If I only access the Context.Request.Files like:
Public Function UploadFilesWithAJAX()
Dim files = Context.Request.Files '<---- Simply adding this... make it works :|
Return "RETURN SOMETHING"
End Function
It works. Hope this helps someone else.
By the way, if someone can explain to me why it works by doing this.
source to share
In your web config you have defined maxRequestLength = "2097151" which is about 2MB, for this reason if you try to upload files over 2MB it will ultimately fail.
Configuration example below (this will allow up to 2GB)
<httpRuntime maxRequestLength="2048576000" executionTimeout="300" />
source to share