5

Problem

I'm trying to upload file using XMLHttpRequest and it seems to work only with small file (such as under 2MO of size). I tried many things so far and came to code shown at the end of the post. But, there is nothing to do; I keep getting the ::ERR_CONNECTION_RESET error. It is not a code issue as under 2MO files are getting unploaded correctly... What am I forgetting? I know this is probably a IIS or web.config issues but I can put my finger on it by googling this problem.

Error given 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>
<!-- [...] -->
Community
  • 1
  • 1
Simon Dugré
  • 17,980
  • 11
  • 57
  • 73

2 Answers2

3

Ok, I solved it...

Solution

If this happen to someone else, be sure to at least access to Context.Request.Files once in your WebService.

Because, during my tests, I was simply :

Public Function UploadFilesWithAJAX()
    Return "RETURN SOMETHING"
End Function

But it was not enough... If I only access to 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 it helps someone else.

By the way, if someone can explain me why it is working by doing this.

Community
  • 1
  • 1
Simon Dugré
  • 17,980
  • 11
  • 57
  • 73
0

In your web config, you defined maxRequestLength="2097151" which is around 2mb, for this reason if you try to upload files more than 2mb it will fail eventually.

Example config below( it will allow up to 2gb )

<httpRuntime maxRequestLength="2048576000" executionTimeout="300" />
Ercan
  • 246
  • 2
  • 8
  • In fact when using it, given error is "La valeur de la propriété 'maxRequestLength' n'est pas valide. L'erreur est : La valeur doit être comprise dans la plage 0-2097151." Sorry, it's in french, but it meant I can only have value from 0 to 2097151. – Simon Dugré Dec 11 '14 at 21:30
  • i see, that means your app pool is running in 32bit mode in that case maxRequestLength value gets measured by KBs so 2097151 is the maximum value as you mentioned. You can ignore my answer for your case and I suggest you to check similar topic here http://stackoverflow.com/questions/4548305/maximum-value-of-maxrequestlength – Ercan Dec 11 '14 at 22:07